Extension inheritance in PHP?

When developing in C#, and you had many classes that used the same exact code, you could rely on another class to hold the generic information, making it easier to modify these classes.

I was wondering if there was anything like that in PHP?

class Dog extends Animal {
    private $animalManager

    public function __construct(AnimalManager $animalManager) { 
        $this->animalManager = $animalManager;
    }
}

class Cat extends Animal {
    private $animalManager

    public function __construct(AnimalManager $animalManager) { 
        $this->animalManager = $animalManager;
    }
}

class Fish extends Animal {
    private $animalManager

    public function __construct(AnimalManager $animalManager) { 
        $this->animalManager = $animalManager;
    }
}

class Animal { 
    // Nothing, yet...
}

What C# would allow you to do is, store the $animalManager and the constructor assignement in the 'Animal' class somehow, making it constant in 1 place if you ever needed to change it.

2 answers

  • answered 2018-02-13 02:01 Aniket Sahrawat

    A draft has been added by Ben Scholzen for generics here.

    But all I can see is type parameters and no wildcards. It supports generic functions and generic constructors. It also supports Bounds.

    Unlike C# and Java, PHP will have its type arguments fully reified, which means we can reflectively know the run time parameter of desired function/constructor.

    Backward compatibility is not a concern here, because type parameters and raw types can never be compatible. So the legacy code won't be compatible with Generics.

  • answered 2018-02-13 02:02 NoobishPro

    The thing is, PHP does this quite neatly.Every extending class inherits everything from the extended class. This means the parent's (in this case animal) construct will run whenever you call one of the extending classes.

    However, you overwrite your parent's class when you call __construct() within the child. Therefore you'd need to specifically call parent::__construct() to run the parent constructor.

    class Animal {
        //Private vars can't be directly accessed by children.
        //You'd have to create a function in the parent return it.
        public $animalManager
    
        //This function will automatically be called if you leave the
        //constructor out of the extended class
        public function __construct($animalManager) { 
            $this->animalManager = $animalManager;
        }
    
        //If you want $animalManager to be private
        //Call like $fish->getAnimalManager();
        //Though I do not see the use of this.
        public function getAnimalManager(){
          return $this->animalManager
        }
    }
    
    class Fish extends Animal {
         //You do not need to do this if you leave the construct out of this class
        public function __construct($animalManager) {
            parent::__construct($animalManager);
            //Do whatever you like here
        }
    }
    

    Example with only the parent constructor:

    class Fish extends Animal {
      //The parent's constructor is called automatically as it's not
      //Being overwritten by this class
      public function test(){
        var_dump($this->animalManager);
      }
    }
    

    Note that you would also not need to initiate the parent class seperately. Just call it like so;

    $fish = new Fish(myAnimalManager);
    $am = $fish->animalManager;
    echo $am;