mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
more details for typed properties
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@350015 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
520d8f432f
commit
a43655118b
1 changed files with 204 additions and 40 deletions
|
@ -4,10 +4,10 @@
|
|||
<title>Properties</title>
|
||||
|
||||
<para>
|
||||
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 <emphasis>properties</emphasis>. You may also see
|
||||
them referred to using other terms such as <emphasis>attributes</emphasis> or
|
||||
<emphasis>fields</emphasis>, but for the purposes of this reference we will use
|
||||
<emphasis>properties</emphasis>. They are defined by using one of the
|
||||
keywords <literal>public</literal>, <literal>protected</literal>,
|
||||
or <literal>private</literal>, optionally followed by a type declaration,
|
||||
followed by a normal variable declaration. This declaration may
|
||||
|
@ -101,15 +101,17 @@ EOD;
|
|||
</para>
|
||||
</note>
|
||||
|
||||
<para>
|
||||
As of PHP 5.3.0
|
||||
<link linkend="language.types.string.syntax.heredoc">heredocs</link> and
|
||||
<link linkend="language.types.string.syntax.nowdoc">nowdocs</link>
|
||||
can be used in any static data context, including property
|
||||
declarations.
|
||||
<example>
|
||||
<title>Example of using a nowdoc to initialize a property</title>
|
||||
<programlisting role="php">
|
||||
<sect2 xml:id="language.oop5.properties.heredoc-nowdoc">
|
||||
<title>Heredoc and Nowdoc</title>
|
||||
<para>
|
||||
As of PHP 5.3.0
|
||||
<link linkend="language.types.string.syntax.heredoc">heredocs</link> and
|
||||
<link linkend="language.types.string.syntax.nowdoc">nowdocs</link>
|
||||
can be used in any static data context, including property
|
||||
declarations.
|
||||
<example>
|
||||
<title>Example of using a nowdoc to initialize a property</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
class foo {
|
||||
|
@ -123,53 +125,215 @@ EOT;
|
|||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<note>
|
||||
<para>
|
||||
Nowdoc and Heredoc support was added in PHP 5.3.0.
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
</note>
|
||||
<note>
|
||||
<para>
|
||||
Nowdoc and Heredoc support was added in PHP 5.3.0.
|
||||
</para>
|
||||
</note>
|
||||
</sect2>
|
||||
|
||||
<para>
|
||||
As of PHP 7.4.0, property definitions can include a type declaration, with the exception of
|
||||
the <literal>callable</literal> type.
|
||||
<example>
|
||||
<title>Example of typed properties</title>
|
||||
<programlisting role="php">
|
||||
<sect2 xml:id="language.oop5.properties.typed-properties">
|
||||
<title>Type declarations</title>
|
||||
<para>
|
||||
As of PHP 7.4.0, property definitions can include a type declaration.
|
||||
<example>
|
||||
<title>Example of typed properties</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
class User
|
||||
{
|
||||
public int $id;
|
||||
public string $name;
|
||||
public ?string $name;
|
||||
|
||||
public function __construct(int $id, string $name)
|
||||
public function __construct(int $id, ?string $name)
|
||||
{
|
||||
$this->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);
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
ID: 1234
|
||||
Name: php
|
||||
int(1234)
|
||||
NULL
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Typed properties must be initialized before accessing, otherwise an
|
||||
<classname>Error</classname> is thrown.
|
||||
<example>
|
||||
<title>Accessing properties</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
class Shape
|
||||
{
|
||||
public int $numberOfSides;
|
||||
public string $name;
|
||||
|
||||
public function setNumberOfSides(int $numberOfSides): void
|
||||
{
|
||||
$this->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());
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
string(8) "triangle"
|
||||
int(3)
|
||||
string(6) "circle"
|
||||
|
||||
Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization
|
||||
]]>
|
||||
</screen>
|
||||
</example>
|
||||
</para>
|
||||
|
||||
<sect3 xml:id="language.oop5.properties.typed-properties.valid-types">
|
||||
<title>Valid property types</title>
|
||||
<informaltable>
|
||||
<tgroup cols="3">
|
||||
<thead>
|
||||
<row>
|
||||
<entry>Type</entry>
|
||||
<entry>Description</entry>
|
||||
<entry>Minimum PHP version</entry>
|
||||
</row>
|
||||
</thead>
|
||||
<tbody>
|
||||
<row>
|
||||
<entry><type>bool</type></entry>
|
||||
<entry>
|
||||
The property must be <type>boolean</type> value.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><type>int</type></entry>
|
||||
<entry>
|
||||
The property must be an <type>integer</type>.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><type>float</type></entry>
|
||||
<entry>
|
||||
The property must be a <type>float</type>ing point number.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><type>string</type></entry>
|
||||
<entry>
|
||||
The property must be a <type>string</type>.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><type>array</type></entry>
|
||||
<entry>
|
||||
The property must be an <type>array</type>.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><literal>object</literal></entry>
|
||||
<entry>
|
||||
The property must be an <type>object</type>.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><literal>iterable</literal></entry>
|
||||
<entry>
|
||||
The property must be either an <type>array</type> or an &instanceof;
|
||||
<interfacename>Traversable</interfacename>.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><literal>self</literal></entry>
|
||||
<entry>
|
||||
The property must be an &instanceof; the same class in which the
|
||||
property is defined.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><literal>parent</literal></entry>
|
||||
<entry>
|
||||
The property must be an &instanceof; the parent class of the class
|
||||
in which the property is defined.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>Class/interface name</entry>
|
||||
<entry>
|
||||
The property must be an &instanceof; the given class or interface
|
||||
name.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>?type</entry>
|
||||
<entry>
|
||||
The property must be the specified type, or &null;.
|
||||
</entry>
|
||||
<entry>PHP 7.4.0</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</informaltable>
|
||||
</sect3>
|
||||
</sect2>
|
||||
|
||||
</sect1>
|
||||
|
||||
|
|
Loading…
Reference in a new issue