From what I gather here you are talking about public instance variables. They are performing as OOP would require. Each time you instantiate a class with
$core = new Core(); // or
$handler = new Handler();
Each of them gets a fresh space in memory to store their instance variables. Instance variables are unique to each instance of a class, as the name would suggest. So, two separate instances of Core and Handler do not share instance variables. However since Handler extends Core, two instances of Core are created. One instance is the one that I created on the first line. The other is created so that Handler can extend it on the second line. These two instances of Core
are not the same object. To have the same values for Core across all core objects you will need to use static (class) variables.
class Core {
public static $hello = 'World';
}
var_dump(Core::$hello); //string('Word')
In my example, $hello
will always be available to everyone by accessing it with the scope resolution operator, ::
. So Handler
could access it with either Core::$hello
or parent::$hello
. If you wanted to only expose this static variable to Core
and its subclasses, then you would need to make it protected
and access it from within Core
with self::$hello
and from its subclasses with parent::$hello
.
class Core {
protected static $hello = 'World';
public function sayHello() {
echo 'Hello '.self::$hello; //from within Core, access with `self`
}
}
class Handler extends Core {
public function myParentSays() {
echo 'My parent says: Hello '.parent::$hello;
}
}
$core = new Core();
$core->sayHello(); // 'Hello World'
$handler = new Handler();
$handler->myParentSays(); // 'My parent says: Hello World'
Check the PHP docs for more on the static keyword and the scope resolution operator.
EDIT
I believe your confusion lies in a misunderstanding of how inheritance works in OOP so let me give you a little real-world-ish example. Let's say you create a class for employees called Employee
. This class has a public instance variable (that is, one that can be accessed with ->
) for the name of the person. In PHP this would be:
class Employee {
public $name;
public __construct($name) {
$this->name = $name;
}
}
Now let's create a new employee:
$tim = new Employee('Tim');
Let's say that we need a new class, Intern
, that should subclass Employee
. That should be easy enough:
class Intern extends Employee {
public function makeCoffee(Employee $receiver) {}
}
If we create a new intern now, should his name be Time just because we have already created another employee named Tim? No. That doesn't make sense.
$intern = new Intern();
var_dump($intern->name); //string(0) ""
Now say that setting the name was some complicated and arduous process and we'd rather not have to code it again. With a little modification to our Intern
class we can leave the name setting to its superclass, Employee
.
class Intern {
public function __construct($name) {
parent::__construct($name);
}
public function makeCoffee(Employee $receiver) {}
}
Now we can create a new intern and set his or her name. Notice how the other Employee keeps his name.
$intern = new Intern('Something Forgettable');
var_dump($intern->name); // string(21) "Something Forgettable"
var_dump($employee->name); // string(3) "Tim"
Now why is this? In OOP, a subclass/superclass is an "is a" relationship. The Intern
"is an" Employee
. The Intern
has all the same properties and methods as an Employee
but because each Intern
and Employee
are distinct they have their own values for these properties.
With this in mind, I suggest you rethink your strategy for your classes. Does it really make sense that Handler
is a Core
? Does it make sense that MainController
is a Handler
?