Been searching all around but still cannot find a solution for this problem.
My problem is that i got these snips of code(Examples):
Core file
class Core {
public $DB = null;
public $Handler = null;
function run() {
$this->DB = "somedatabase";
include_once('handler.php');
$this->Handler = new Handler;
$this->Handler->run();
}
}
This is the helper.php example
class Handler extends Core {
function run() {
echo "<pre>"; print_r($this); echo "</pre>"; die();
}
}
Even tho i defined the DB variable before i include the helper then it is still empty inside the helper class. It’s defined yes but it’s empty. Which means it properly doesn’t share the same memory as the Core class.
Keep in mind that the Core class it self is instanced too.
–
Thanks for all suggestions
Edit
PhpMyCoder got it right. Thank you for the detailed and well written reply.
For over 2 years i been seeing PHP scopes as being the same or sorta the same as JavaScript’s scope. Now i realize that if i extend my “Core” class i get all the methods and properties within it. But the values is private to my class and my class alone.
This is great. Finally i got it.
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
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
Coreare not the same object. To have the same values for Core across all core objects you will need to use static (class) variables.In my example,
$hellowill always be available to everyone by accessing it with the scope resolution operator,::. SoHandlercould access it with eitherCore::$helloorparent::$hello. If you wanted to only expose this static variable toCoreand its subclasses, then you would need to make itprotectedand access it from withinCorewithself::$helloand from its subclasses withparent::$hello.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:Now let’s create a new employee:
Let’s say that we need a new class,
Intern, that should subclassEmployee. That should be easy enough: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.
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
Internclass we can leave the name setting to its superclass,Employee.Now we can create a new intern and set his or her name. Notice how the other Employee keeps his name.
Now why is this? In OOP, a subclass/superclass is an “is a” relationship. The
Intern“is an”Employee. TheInternhas all the same properties and methods as anEmployeebut because eachInternandEmployeeare 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
Handleris aCore? Does it make sense thatMainControlleris aHandler?