Weird behavior for $this variable in PHP4

php.internals

Unnamed Person

23 years ago
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{ var $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(); ?>

Alan Knowles

23 years ago
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{ > var $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(); > > ?> > >
-- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com