From a43655118be85f174912b5222eb6ba6902b29f81 Mon Sep 17 00:00:00 2001 From: Peter Cowburn Date: Mon, 8 Jun 2020 20:49:01 +0000 Subject: [PATCH] more details for typed properties git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@350015 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/properties.xml | 244 +++++++++++++++++++++++++++++------ 1 file changed, 204 insertions(+), 40 deletions(-) diff --git a/language/oop5/properties.xml b/language/oop5/properties.xml index 67ceca45cd..45aaacb142 100644 --- a/language/oop5/properties.xml +++ b/language/oop5/properties.xml @@ -4,10 +4,10 @@ Properties - Class member variables are called "properties". You may also see - them referred to using other terms such as "attributes" or - "fields", but for the purposes of this reference we will use - "properties". They are defined by using one of the + Class member variables are called properties. You may also see + them referred to using other terms such as attributes or + fields, but for the purposes of this reference we will use + properties. They are defined by using one of the keywords public, protected, or private, optionally followed by a type declaration, followed by a normal variable declaration. This declaration may @@ -101,15 +101,17 @@ EOD; - - As of PHP 5.3.0 - heredocs and - nowdocs - can be used in any static data context, including property - declarations. - - Example of using a nowdoc to initialize a property - + + Heredoc and Nowdoc + + As of PHP 5.3.0 + heredocs and + nowdocs + can be used in any static data context, including property + declarations. + + Example of using a nowdoc to initialize a property + ]]> - - - - - - Nowdoc and Heredoc support was added in PHP 5.3.0. + + - + + + Nowdoc and Heredoc support was added in PHP 5.3.0. + + + - - As of PHP 7.4.0, property definitions can include a type declaration, with the exception of - the callable type. - - Example of typed properties - + + Type declarations + + As of PHP 7.4.0, property definitions can include a type declaration. + + Example of typed properties + id = $id; $this->name = $name; } } -$user = new User(1234, "php"); -echo "ID: " . $user->id; -echo "\n"; -echo "Name: " . $user->name; +$user = new User(1234, null); + +var_dump($user->id); +var_dump($user->name); ?> ]]> - - &example.outputs; - + + &example.outputs; + - - - + + + + + + Typed properties must be initialized before accessing, otherwise an + Error is thrown. + + Accessing properties + +numberOfSides = $numberOfSides; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getNumberOfSides(): int + { + return $this->numberOfSides; + } + + public function getName(): string + { + return $this->name; + } +} + +$triangle = new Shape(); +$triangle->setName("triangle"); +$triangle->setNumberofSides(3); +var_dump($triangle->getName()); +var_dump($triangle->getNumberOfSides()); + +$circle = new Shape(); +$circle->setName("circle"); +var_dump($circle->getName()); +var_dump($circle->getNumberOfSides()); +?> +]]> + + &example.outputs; + + + + + + + + Valid property types + + + + + Type + Description + Minimum PHP version + + + + + bool + + The property must be boolean value. + + PHP 7.4.0 + + + int + + The property must be an integer. + + PHP 7.4.0 + + + float + + The property must be a floating point number. + + PHP 7.4.0 + + + string + + The property must be a string. + + PHP 7.4.0 + + + array + + The property must be an array. + + PHP 7.4.0 + + + object + + The property must be an object. + + PHP 7.4.0 + + + iterable + + The property must be either an array or an &instanceof; + Traversable. + + PHP 7.4.0 + + + self + + The property must be an &instanceof; the same class in which the + property is defined. + + PHP 7.4.0 + + + parent + + The property must be an &instanceof; the parent class of the class + in which the property is defined. + + PHP 7.4.0 + + + Class/interface name + + The property must be an &instanceof; the given class or interface + name. + + PHP 7.4.0 + + + ?type + + The property must be the specified type, or &null;. + + PHP 7.4.0 + + + + + +