php-doc-en/language/oop5/properties.xml
Tyson Andre 02f3bf5717 $this is not a **reference** to the called object.
It's the value of the called object.

Additionally, in php 7.1, it was no longer possible to reassign $this
indirectly through a reference.

Even in php 7.0, I'd think it would initially be more like a value
(that references can be created from) than
a reference, but I may be misunderstanding what this means or what $this
did in php 5 or older.

Closes GH-380.
2021-01-24 15:56:27 +01:00

213 lines
5.4 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>.
They may be referred to using other terms such as <emphasis>fields</emphasis>,
but for the purposes of this reference <emphasis>properties</emphasis>
will be used. They are defined by using one of the keywords
<literal>public</literal>, <literal>protected</literal>, or
<literal>private</literal>, optionally, as of PHP 7.4,
followed by a type declaration, followed by a normal variable declaration.
This declaration may include an initialization, but this initialization
must be a <link linkend="language.constants">constant</link> value.
</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>
An alternative and not recommended way of declaring class properties, as it is to maintain backward
compatibility with PHP 4, is by using
the <literal>var</literal> keyword.
It will treat the property identically as it would have 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 the value of 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
{
public $var1 = 'hello ' . 'world';
public $var2 = <<<EOD
hello world
EOD;
public $var3 = 1+2;
// invalid property declarations:
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
// valid property declarations:
public $var6 = myConstant;
public $var7 = [true, false];
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.typed-properties">
<title>Type declarations</title>
<para>
As of PHP 7.4.0, property definitions can include a
<xref linkend="language.types.declarations" />,
with the exception of <type>callable</type>.
<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>
</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
-->