Forum Moderators: coopster
class MyBaseClass {
protected $_myBaseProperty = null;
public function __construct() {
$this->_myBaseProperty = 'Hello';
}
// Outputs 'Hello World' when called from an instance of MyExtendedClass
// - although I was expecting an error since isn't inheritance in reverse?!
// (Certainly an error if called from an instance of MyBaseClass)
public function myBaseMethod() {
echo $this->_myBaseProperty.' '.$this->_myExtendedProperty;
}
}
class MyExtendedClass extends MyBaseClass {
protected $_myExtendedProperty = null;
public function __construct() {
parent::__construct();
$this->_myExtendedProperty = 'World';
}
// Outputs 'Hello World' as expected
public function myExtendedMethod() {
echo $this->_myBaseProperty.' '.$this->_myExtendedProperty;
}
}
$MyExtendedInstance = new MyExtendedClass;
// Outputs 'Hello World' - although I was expecting an error!
$MyExtendedInstance->myBaseMethod();
This, IMO, goes against what actual inheritance is and is an example of poor programming. There should never be a case when a class is referencing variables outside its declared scope. Sure there are times when global variables may be helpful, but it is still poor programming practice.
// 'Hello World' when called from an instance of MyExtendedClass
// Or just 'Hello' when called from an instance of MyBaseClass
public function myBaseMethod() {
if (property_exists($this,'_myExtendedProperty')) {
echo $this->_myBaseProperty.' '.$this->_myExtendedProperty;
} else {
echo $this->_myBaseProperty.' (_myExtendedProperty does not exist)';
}
}
class MyExtendedClass extends MyBaseClass
class MyBaseClass
{
public function a { return 1; }
}
class MyExtendedClass extends MyBaseClass
{
public function b { return 2; }
}
MyBaseClass has one funtion, called a. MyExtendedClass has two functions, a and b. Is that not correct?
I understand that by extending, you are basically taking a copy of the class into the new class, thus all variables between the two are shared.
class MyBaseClass {
public function a() { return 1; }
}
class MyExtendedClass extends MyBaseClass {
public function a() {
parent::a(); // Calls MyBaseClass::a() but not statically
return 3;
}
public function b() { return 2; }
} Does this work?:
$MyExtendedInstance = new MyExtendedClass; // MyExtendedClass
$MyExtendedInstance->myBaseMethod(); // should run fine as variables are shared
$MyExtendedInstance = new MyBaseClass; // Changed to mybaseclass
$MyExtendedInstance->myBaseMethod(); // should cause error because $this->_myExtendedProperty doesn't exist in MyBaseClass?
In your example, therefore, MyExtendedClass should have access to the variables you declare in MyBaseClass and MyBaseClass should have access to variables inside MyExtended class, but only when called through MyExtendedClass.
$MyExtendedInstance = new MyExtendedClass; // MyExtendedClass
$MyExtendedInstance->myBaseMethod(); // should run fine as variables are shared
public function myBaseMethod() {
print '<pre>'; var_dump($this->_myExtendedProperty); print '</pre>';
echo $this->_myBaseProperty.' '.$this->_myExtendedProperty;
}
.
.
.
$MyBaseInstance = new MyBaseClass();
$MyBaseInstance->myBaseMethod();
Notice: Undefined property: MyBaseClass::$_myExtendedProperty in test.php on line 12
NULL
Notice: Undefined property: MyBaseClass::$_myExtendedProperty in test.php on line 13
Hello