note 77230 added to language.oop5.basic

From: Date: Tue, 21 Aug 2007 03:13:10 +0000
Subject: note 77230 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-129657@lists.php.net to get a copy of this message
$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(); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 222.128.39.136 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/77230 Del: integrated -- https://master.php.net/note/delete/77230/integrated Del: useless -- https://master.php.net/note/delete/77230/useless Del: bad code -- https://master.php.net/note/delete/77230/bad+code Del: spam -- https://master.php.net/note/delete/77230/spam Del: non-english -- https://master.php.net/note/delete/77230/non-english Del: in docs -- https://master.php.net/note/delete/77230/in+docs Del: other reasons-- https://master.php.net/note/delete/77230 Reject -- https://master.php.net/note/reject/77230 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129657) next »