sean Sat Jan 29 01:19:15 2005 EDT
Modified files: /phpdoc/en/language oop.xml Log: document seemingly-odd $this behaviour http://cvs.php.net/diff.php/phpdoc/en/language/oop.xml?r1=1.57&r2=1.58&ty=u Index: phpdoc/en/language/oop.xml diff -u phpdoc/en/language/oop.xml:1.57 phpdoc/en/language/oop.xml:1.58 --- phpdoc/en/language/oop.xml:1.57 Sun Jan 23 15:04:35 2005 +++ phpdoc/en/language/oop.xml Sat Jan 29 01:19:14 2005 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.57 $ --> +<!-- $Revision: 1.58 $ --> <chapter id="language.oop"> <title>Classes and Objects (PHP 4)</title> @@ -246,6 +246,62 @@ <note> <para> + The <varname>$this</varname> pseudo-variable is not usually defined if + the method in which it is hosted is called statically. This is not, + however, a strict rule: <varname>$this</varname> is defined if a method is + called statically from within another object, and the value of + <varname>$this</varname> is that of the calling object. This is + illustrated in the following example: + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php +class A +{ + function foo() + { + if (isset($this)) { + echo '$this is defined ('; + echo get_class($this); + echo ")\n"; + } else { + echo "\$this is not defined.\n"; + } + } +} + +class B +{ + function bar() + { + A::foo(); + } +} + +$a = new A(); +$a->foo(); +A::foo(); +$b = new B(); +$b->bar(); +B::bar(); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +$this is defined (a) +$this is not defined. +$this is defined (b) +$this is not defined. +]]> + </screen> + </informalexample> + </para> + </note> + + <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>.