Re: Weird behavior for $this variable in PHP4
| From: | Alan Knowles | Date: | Sat, 12 Jul 2003 00:54:25 +0000 |
| Subject: | Re: Weird behavior for $this variable in PHP4 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-3373@lists.php.net to get a copy of this message | ||
in php4 $this gets passed in All static method calls ,as if they where parent::sss() - so statically calling one object method from another object instance will take '$this' with it..
Last word on this 'feature/bug' is that it will be removed in PHP5, (bit of a bugger - I'm sure some of my old code relied on it ;)
Regards
Alan
Josh Fuhs wrote:
Hello, I'm getting a problem with $this being defined where it shouldn't be. The code that I'm writing is a little weird in order to compensate for the lack of some OO features in PHP4. The PHP script at the bottom should give the behavior I'm referring to. Is PHP5 in a more-or-less stable state? Are PHP4 extensions completely compatible with PHP5? Josh <?php /* No static attributes in PHP4. */ $static_class1=NULL; class Class1{-- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.comvar $var1; function Class1(){ $this->var1=0; }function init(){ global $static_class1;$static_class1 = new Class1();}function getVar(){ /* Check for $this. This is a way of making a static method and a regular method with the same name. */ /* $this is defined here directly after called from the Class2 constructor. */ if(isset($this)){ return $this->var1;} else {global $static_class1; $static_class1->getVar();}}} class Class2{function Class2(){ $a=Class1::getVar(); }} Class1::init(); $a=new Class2(); ?>