php-doc-en/language/oop5/properties.xml
Peter Cowburn a43655118b more details for typed properties
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@350015 c90b9560-bf6c-de11-be94-00142212c4b1
2020-06-08 20:49:01 +00:00

359 lines
9.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.oop5.properties" xmlns="http://docbook.org/ns/docbook">
<title>Properties</title>
<para>
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
include an initialization, but this initialization must be a constant
value--that is, it must be able to be evaluated at compile time and
must not depend on run-time information in order to be evaluated.
</para>
<para>
See <xref linkend="language.oop5.visibility" /> for more
information on the meanings
of <literal>public</literal>, <literal>protected</literal>,
and <literal>private</literal>.
</para>
<note>
<para>
In order to maintain backward compatibility with PHP 4, PHP 5 will
still accept the use of the keyword <literal>var</literal> in
property declarations instead of (or in addition
to) <literal>public</literal>, <literal>protected</literal>,
or <literal>private</literal>. However, <literal>var</literal> is
no longer required. In versions of PHP from 5.0 to 5.1.3, the use
of <literal>var</literal> was considered deprecated and would
issue an <constant>E_STRICT</constant> warning, but since PHP
5.1.3 it is no longer deprecated and does not issue the warning.
</para>
<para>
If you declare a property using <literal>var</literal> instead of
one of <literal>public</literal>, <literal>protected</literal>,
or <literal>private</literal>, then PHP 5 will treat the property
as if it had been declared as <literal>public</literal>.
</para>
</note>
<para>
Within class methods non-static properties may be accessed by using
<literal>-&gt;</literal> (Object Operator): <varname>$this-&gt;property</varname>
(where <literal>property</literal> is the name of the property).
Static properties are accessed by using the <literal>::</literal> (Double Colon):
<varname>self::$property</varname>. See <link linkend="language.oop5.static">Static Keyword</link>
for more information on the difference between static and non-static properties.
</para>
<para>
The pseudo-variable <varname>$this</varname> is available inside
any class method when that method is called from within an object
context. <varname>$this</varname> is a reference to the calling
object (usually the object to which the method belongs, but
possibly another object, if the method is called
<link linkend="language.oop5.static">statically</link> from the context
of a secondary object).
</para>
<para>
<example>
<title>property declarations</title>
<programlisting role="php">
<![CDATA[
<?php
class SimpleClass
{
// valid as of PHP 5.6.0:
public $var1 = 'hello ' . 'world';
// valid as of PHP 5.3.0:
public $var2 = <<<EOD
hello world
EOD;
// valid as of PHP 5.6.0:
public $var3 = 1+2;
// invalid property declarations:
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
// valid property declarations:
public $var6 = myConstant;
public $var7 = array(true, false);
// valid as of PHP 5.3.0:
public $var8 = <<<'EOD'
hello world
EOD;
}
?>
]]>
</programlisting>
</example>
</para>
<note>
<para>
There are some nice functions to handle classes and objects. You
might want to take a look at
the <link linkend="ref.classobj">Class/Object Functions</link>.
</para>
</note>
<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 {
// As of PHP 5.3.0
public $bar = <<<'EOT'
bar
EOT;
public $baz = <<<EOT
baz
EOT;
}
?>
]]>
</programlisting>
</example>
</para>
<note>
<para>
Nowdoc and Heredoc support was added in PHP 5.3.0.
</para>
</note>
</sect2>
<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 function __construct(int $id, ?string $name)
{
$this->id = $id;
$this->name = $name;
}
}
$user = new User(1234, null);
var_dump($user->id);
var_dump($user->name);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
int(1234)
NULL
]]>
</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>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->