<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Properties</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.basic.html">The Basics</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.constants.html">Class Constants</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.oop5.properties" class="sect1"> <h2 class="title">Properties</h2> <p class="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 <em>public</em>, <em>protected</em>, or <em>private</em>, 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. </p> <p class="para"> See <a href="language.oop5.visibility.html" class="xref">Visibility</a> for more information on the meanings of <em>public</em>, <em>protected</em>, and <em>private</em>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword <em>var</em> in property declarations instead of (or in addition to) <em>public</em>, <em>protected</em>, or <em>private</em>. However, <em>var</em> is no longer required. In versions of PHP from 5.0 to 5.1.3, the use of <em>var</em> was considered deprecated and would issue an <strong><code>E_STRICT</code></strong> warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning. </p> <p class="para"> If you declare a property using <em>var</em> instead of one of <em>public</em>, <em>protected</em>, or <em>private</em>, then PHP 5 will treat the property as if it had been declared as <em>public</em>. </p> </p></blockquote> <p class="para"> Within class methods non-static properties may be accessed by using <em>-></em> (Object Operator): <var class="varname"><var class="varname">$this->property</var></var> (where <em>property</em> is the name of the property). Static properties are accessed by using the <em>::</em> (Double Colon): <var class="varname"><var class="varname">self::$property</var></var>. See <a href="language.oop5.static.html" class="link">Static Keyword</a> for more information on the difference between static and non-static properties. </p> <p class="para"> The pseudo-variable <var class="varname"><var class="varname">$this</var></var> is available inside any class method when that method is called from within an object context. <var class="varname"><var class="varname">$this</var></var> is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called <a href="language.oop5.static.html" class="link">statically</a> from the context of a secondary object). </p> <p class="para"> <div class="example" id="example-176"> <p><strong>Example #1 property declarations</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">SimpleClass<br /></span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">// invalid property declarations:<br /> </span><span style="color: #007700">public </span><span style="color: #0000BB">$var1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'hello ' </span><span style="color: #007700">. </span><span style="color: #DD0000">'world'</span><span style="color: #007700">;<br /> public </span><span style="color: #0000BB">$var2 </span><span style="color: #007700">= <<<EOD<br /></span><span style="color: #DD0000">hello world<br /></span><span style="color: #007700">EOD;<br /> public </span><span style="color: #0000BB">$var3 </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /> public </span><span style="color: #0000BB">$var4 </span><span style="color: #007700">= </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">myStaticMethod</span><span style="color: #007700">();<br /> public </span><span style="color: #0000BB">$var5 </span><span style="color: #007700">= </span><span style="color: #0000BB">$myVar</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">// valid property declarations:<br /> </span><span style="color: #007700">public </span><span style="color: #0000BB">$var6 </span><span style="color: #007700">= </span><span style="color: #0000BB">myConstant</span><span style="color: #007700">;<br /> public </span><span style="color: #0000BB">$var7 </span><span style="color: #007700">= array(</span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">// This is allowed only in PHP 5.3.0 and later.<br /> </span><span style="color: #007700">public </span><span style="color: #0000BB">$var8 </span><span style="color: #007700">= <<<'EOD'<br /></span><span style="color: #DD0000">hello world<br /></span><span style="color: #007700">EOD;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> There are some nice functions to handle classes and objects. You might want to take a look at the <a href="ref.classobj.html" class="link">Class/Object Functions</a>. </p> </p></blockquote> <p class="para"> Unlike <a href="language.types.string.html#language.types.string.syntax.heredoc" class="link">heredocs</a>, <a href="language.types.string.html#language.types.string.syntax.nowdoc" class="link">nowdocs</a> can be used in any static data context, including property declarations. <div class="example" id="example-177"> <p><strong>Example #2 Example of using a nowdoc to initialize a property</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo </span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">// As of PHP 5.3.0<br /> </span><span style="color: #007700">public </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= <<<'EOT'<br /></span><span style="color: #DD0000">bar<br /></span><span style="color: #007700">EOT;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Nowdoc support was added in PHP 5.3.0. </p> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.basic.html">The Basics</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.constants.html">Class Constants</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>