mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-18 18:08:54 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@328167 c90b9560-bf6c-de11-be94-00142212c4b1
149 lines
4.7 KiB
XML
149 lines
4.7 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 "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 <literal>public</literal>, <literal>protected</literal>,
|
|
or <literal>private</literal>, 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>-></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>
|
|
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
|
|
{
|
|
// invalid property declarations:
|
|
public $var1 = 'hello ' . 'world';
|
|
public $var2 = <<<EOD
|
|
hello world
|
|
EOD;
|
|
public $var3 = 1+2;
|
|
public $var4 = self::myStaticMethod();
|
|
public $var5 = $myVar;
|
|
|
|
// valid property declarations:
|
|
public $var6 = myConstant;
|
|
public $var7 = array(true, false);
|
|
|
|
// This is allowed only in PHP 5.3.0 and later.
|
|
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>
|
|
|
|
<para>
|
|
Unlike
|
|
<link linkend="language.types.string.syntax.heredoc">heredocs</link>,
|
|
<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;
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Nowdoc support was added in PHP 5.3.0.
|
|
</para>
|
|
</note>
|
|
</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
|
|
-->
|