Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From what I gather here you are talking about public <em>instance</em> variables. They are performing as OOP would require. Each time you instantiate a class with</p> <pre><code>$core = new Core(); // or $handler = new Handler(); </code></pre> <p>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 <code>Core</code> are not the same object. To have the same values for Core across all core objects you will need to use static (class) variables.</p> <pre><code>class Core { public static $hello = 'World'; } var_dump(Core::$hello); //string('Word') </code></pre> <p>In my example, <code>$hello</code> will always be available to everyone by accessing it with the scope resolution operator, <code>::</code>. So <code>Handler</code> could access it with either <code>Core::$hello</code> or <code>parent::$hello</code>. If you wanted to only expose this static variable to <code>Core</code> and its subclasses, then you would need to make it <code>protected</code> and access it from within <code>Core</code> with <code>self::$hello</code> and from its subclasses with <code>parent::$hello</code>.</p> <pre><code>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-&gt;sayHello(); // 'Hello World' $handler = new Handler(); $handler-&gt;myParentSays(); // 'My parent says: Hello World' </code></pre> <p>Check the PHP docs for more on the <a href="http://www.php.net/manual/en/language.oop5.static.php" rel="noreferrer">static keyword</a> and the <a href="http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php" rel="noreferrer">scope resolution operator</a>.</p> <hr> <p><strong>EDIT</strong><br> 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 <code>Employee</code>. This class has a public instance variable (that is, one that can be accessed with <code>-&gt;</code>) for the name of the person. In PHP this would be:</p> <pre><code>class Employee { public $name; public __construct($name) { $this-&gt;name = $name; } } </code></pre> <p>Now let's create a new employee:</p> <pre><code>$tim = new Employee('Tim'); </code></pre> <p>Let's say that we need a new class, <code>Intern</code>, that should subclass <code>Employee</code>. That should be easy enough:</p> <pre><code>class Intern extends Employee { public function makeCoffee(Employee $receiver) {} } </code></pre> <p>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.</p> <pre><code>$intern = new Intern(); var_dump($intern-&gt;name); //string(0) "" </code></pre> <p>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 <code>Intern</code> class we can leave the name setting to its superclass, <code>Employee</code>.</p> <pre><code>class Intern { public function __construct($name) { parent::__construct($name); } public function makeCoffee(Employee $receiver) {} } </code></pre> <p>Now we can create a new intern and set his or her name. Notice how the other Employee keeps his name.</p> <pre><code>$intern = new Intern('Something Forgettable'); var_dump($intern-&gt;name); // string(21) "Something Forgettable" var_dump($employee-&gt;name); // string(3) "Tim" </code></pre> <p>Now why is this? In OOP, a subclass/superclass is an <em>"is a"</em> relationship. The <code>Intern</code> "is an" <code>Employee</code>. The <code>Intern</code> has all the same properties and methods as an <code>Employee</code> but because each <code>Intern</code> and <code>Employee</code> are distinct they have their own values for these properties.</p> <p>With this in mind, I suggest you rethink your strategy for your classes. Does it really make sense that <code>Handler</code> is a <code>Core</code>? Does it make sense that <code>MainController</code> is a <code>Handler</code>? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload