php-doc-en/language/oop5/properties.xml
Torben Wilson af4410a7e1 Normalized the sgml-default-dtd-file local-variable line for those
still using this, after discussion on the phpdoc list.
From now on, manual.ced will need to be found at ~/.phpdoc/manual.ced.



git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@288721 c90b9560-bf6c-de11-be94-00142212c4b1
2009-09-25 07:04:39 +00:00

150 lines
4.6 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $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 the properties, constants, and methods may be
accessed by using the form <varname>$this-&gt;property</varname>
(where <literal>property</literal> is the name of the property)
unless the access is to a static property within the context of a
static class method, in which case it is accessed using the
form <varname>self::$property</varname>. See <link linkend="language.oop5.static">Static
Keyword</link> for more information.
</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
-->