note 77230 deleted from language.oop5.basic by nlopess
| From: | nlopess@php.net | 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();
?>