:: context access (bug?)

php.internals

Marc Dembogurski

22 years ago
I?ve found the example below which took my attention. The call to cCat::Miew() assumes the cDog context and is able to access $this of cDog. Is this supposed to be a bug or something that was not expected ????? It happens to PHP4 to and php5. And looks a bit strange... class cCat { function Miew(){ // cCat does not have a member "kind", but cDog has, and we'll use it echo "I am ".$this->kind.", and I say MIEW\n"; // here things are even stranger: does cCat class // support WhoAmI function? guess again... $this->WhoAmI(); } } class cDog { public $kind = "DOG"; function Bark(){ // let's make this dog act like a cat:) cCat::Miew(); } function WhoAmI(){ echo "Yes, I'm really ".$this->kind."!"; } } $dog = new cDog(); echo $dog->Bark(); //outputs: //I am DOG, and I say MIEW //Yes, I'm really DOG! __________________________________ Do you Yahoo!? Free Pop-Up Blocker - Get it now http://companion.yahoo.com/

Andi Gutmans

22 years ago
This is a "feature". We decided to keep it the way it is, and have people who really want to code correctly define their static methods as static. So to fix this you should define Miew() as static and you won't have access to the calling scope. Andi At 11:56 AM 12/4/2003 -0800, Marc Dembogurski wrote: