mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 08:28:54 +00:00
Properties do not need to be declared with a visibility modifier (#1311)
Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
This commit is contained in:
parent
b9d5dcd782
commit
f5e5b54129
2 changed files with 20 additions and 18 deletions
|
@ -7,34 +7,33 @@
|
|||
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,
|
||||
will be used. They are defined by using at least one modifier (such as
|
||||
<xref linkend="language.oop5.visibility"/>,
|
||||
<xref linkend="language.oop5.static"/>,
|
||||
or, as of PHP 8.1.0, <code>readonly</code>),
|
||||
optionally (except for <code>readonly</code> properties), 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>.
|
||||
An obsolete way of declaring class properties, is by using the
|
||||
<literal>var</literal> keyword instead of a modifier.
|
||||
</para>
|
||||
</note>
|
||||
<note>
|
||||
<simpara>
|
||||
A property declared without a <xref linkend="language.oop5.visibility"/>
|
||||
modifier will be declared as <literal>public</literal>.
|
||||
</simpara>
|
||||
</note>
|
||||
<para>
|
||||
Within class methods non-static properties may be accessed by using
|
||||
<literal>-></literal> (Object Operator): <varname>$this->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>
|
||||
<varname>self::$property</varname>. See <xref linkend="language.oop5.static" />
|
||||
for more information on the difference between static and non-static properties.
|
||||
</para>
|
||||
<para>
|
||||
|
@ -68,6 +67,9 @@ EOD;
|
|||
hello world
|
||||
EOD;
|
||||
}
|
||||
// Without visibility modifier:
|
||||
static $var9;
|
||||
readonly int $var10;
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
<sect2 xml:id="language.oop5.visibility-members">
|
||||
<title>Property Visibility</title>
|
||||
<para>
|
||||
Class properties must be defined as public, private, or
|
||||
protected. If declared using <literal>var</literal>,
|
||||
the property will be defined as public.
|
||||
Class properties may be defined as public, private, or
|
||||
protected. Properties declared without any explicit visibility
|
||||
keyword are defined as public.
|
||||
</para>
|
||||
<para>
|
||||
<example>
|
||||
|
|
Loading…
Reference in a new issue