note 77230 deleted from language.oop5.basic by nlopess

From: Date: Sat, 01 Sep 2007 12:03:10 +0000
Subject: note 77230 deleted from language.oop5.basic by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-130008@lists.php.net to get a copy of this message
Note Submitter: dulao5 Reason: non-english ---- $this 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 statically from the context of a secondary object). note: 注意是“静态调用”而非静态方法。 经过测试,静态方法绝对不会有this伪变量。 #!/usr/local/bin/php <?php class ClassB{ const NAME = "Class B"; public $a = ClassB::NAME; public function foo(){ //当这个方法被对象$a静态调用时,this==$a echo $this->a.":: foo call\n"; echo "'this' is a {$this->a} object\n"; } }; class ClassA{ const NAME = "class A"; public $a = ClassA::NAME; public function createB(){ return new ClassB(); } public function foo(){ echo $this->a.":: foo call\n"; echo "\n\nCall a object function\n"; $this->createB()->foo(); echo "\n\nthe method is called statically from \$A\n"; ClassB::foo(); } } $A = new ClassA(); $A->foo(); ?>

« previous php.notes (#130008) next »