Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8929

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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 &quot;properties&quot;. You may also see
   them referred to using other terms such as &quot;attributes&quot; or
   &quot;fields&quot;, but for the purposes of this reference we will use
   &quot;properties&quot;. 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>-&gt;</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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">SimpleClass<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;invalid&nbsp;property&nbsp;declarations:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$var1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'hello&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'world'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var2&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">hello&nbsp;world<br /></span><span style="color: #007700">EOD;<br />&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var3&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var4&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">myStaticMethod</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var5&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$myVar</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;valid&nbsp;property&nbsp;declarations:<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$var6&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">myConstant</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var7&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;This&nbsp;is&nbsp;allowed&nbsp;only&nbsp;in&nbsp;PHP&nbsp;5.3.0&nbsp;and&nbsp;later.<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$var8&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOD'<br /></span><span style="color: #DD0000">hello&nbsp;world<br /></span><span style="color: #007700">EOD;<br />}<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;As&nbsp;of&nbsp;PHP&nbsp;5.3.0<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOT'<br /></span><span style="color: #DD0000">bar<br /></span><span style="color: #007700">EOT;<br />}<br /></span><span style="color: #0000BB">?&gt;</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>