Weird behavior for $this variable in PHP4
| From: | Josh Fuhs | Date: | Fri, 11 Jul 2003 21:38:32 +0000 |
| Subject: | Weird behavior for $this variable in PHP4 | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-3368@lists.php.net to get a copy of this message | ||
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();
?>