sean Sat Jan 29 09:43:56 2005 EDT
Modified files: /phpdoc/en/language/oop5 basic.xml Log: document seemingly-odd $this behaviour http://cvs.php.net/diff.php/phpdoc/en/language/oop5/basic.xml?r1=1.12&r2=1.13&ty=u Index: phpdoc/en/language/oop5/basic.xml diff -u phpdoc/en/language/oop5/basic.xml:1.12 phpdoc/en/language/oop5/basic.xml:1.13 --- phpdoc/en/language/oop5/basic.xml:1.12 Mon Jan 24 03:23:35 2005 +++ phpdoc/en/language/oop5/basic.xml Sat Jan 29 09:43:56 2005 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.12 $ --> +<!-- $Revision: 1.13 $ --> <sect1 id="language.oop5.basic"> <title>The Basics</title> @@ -10,11 +10,58 @@ Every class definition begins with the keyword class, followed by a class name, which can be any name that isn't a <link linkend="reserved">reserved</link> word in PHP. Followed by a pair of curly braces, of - which contains the definition of the classes members and methods. Within - each method, except for <link linkend="language.oop5.static">static</link> - methods, a pseudo variable <varname>$this</varname> is available. - <varname>$this</varname> is a reference to the same instance that - called the method. + which contains the definition of the classes members and methods. A + pseudo-variable, <varname>$this</varname> is available when a 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 can be another object, if the method is called + <link linkend="language.oop5.static">statically</link> from the context + of a secondary 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> <example> <title>Simple Class definition</title>