Class inheritence

Its rare for me to post threads but having use procedural PHP for so long i forgot a lot about OOP inheritance among other things, currently i am working on a personal project that uses a parent class to setup things such as language arrays, smarty and a couple of other things.

I understand the basics behind extending parents within child classes but what i can’t work my head around is when you introduce another class to the parent, the issue i can’t work through my head is extending the parent to this new class causes all the properties to reset to there defaults even though the class has run through its setup cycle.

So you get a better idea of what I’m talking about see the example below…

class Parent {
    public $talk    = 'hi';
    public $init    = false;
    public function __instance() {
        $this->talk = 'welcome';
    public function __someFunction() {
        $this->init = new Other;

class Child extends Parent {
    public function __construct() {

// --> some_other_class.php
class Other extends Parent {
    public function alert() {

So basically the way i setup my classes is i created a function called __instance which builds the property values that the Child class can use fine with no issues. The problem is when the Other class is introduced that all the properties are inherited but are not set like they are in the Child class and because the Other class is shared between the front and back end of the code i can’t extend off the Child class without creating 2 files.

I hope my explanation of what I’m attempting is clear as when I and OOP meet we can never play nice anymore.

Thanks in advance.

I have no idea what you’re trying to do (never dove that deep in OOP i guess), but as far as I know, you can’t change the default properties of a class. You can change the properties of an instance.

Every instance of a class created will always start with the default properties of the class.

Correct me if I’m wrong.

I suck as explaining things unless I’m on Skype or Teamspeak, basically what I’m attempting is allowing each child class of the parent class the ability to inherit the property values set during the initial setup rather then the defaults re-appearing. So far in the example above the child class inherits the value of “welcome” fine but the second i introduce the class that gets included from the first child all the properties reset giving me “hi” instead.

The main reason I’m extending off of each class is because i want to have the parent do all the work as far as the initial setup that way the children classes just extend and use the properties and methods rather then writing helpers methods to set the correct configuration which I’m currently been forced to do.

Hopefully that clears it up a little bit more otherwise see the below…

Child > Parent = Inherits all properties correctly
include(Child) > Parent = Inherits only the defaults of the properties

No you explained well, I understood your problem.
I guess it’s me that doesn’t explain well.

What you want (AFAIK) is not possible. What I am trying to say is, you have to keep in mind the difference between class and instance.

Once you’ve created an instance of a class, then you can set the property values of that instance.

But you can’t change the property values of a class, and then create an instance of that class (or a child class) which automatically inherits those changed property values. At the moment of instantiation, the new instance will always have the default values.

In your example, Child does too. But then you call the method that changes them (after the instantiation).

Ahh that makes sense, i guess the only option i have then will be to separate the Smarty instance from the parent __instance and call it once within the child class it needs to be used in. Not a big change but i was really hoping that children inherited property values once a new instance of the parent was called but i guess i learn something new everyday.

Thanks Guido :slight_smile:

You are wrong.

Class A {
  public $foo = 'Hello';

$a = new A();
echo $a->foo; // Hello.

Class B extends A {
  public $foo = 'Howdy';

$b = new B();
echo $b->foo; // Howdy.

That’s pretty fundamental. Child classes can modify the values of any public or protected property (and I think private as well, but I almost never use private so I can’t say for certain if this is so) of the parent both at declaration or when they are running. Further, properties have only one value.