-2

I have an inheritance issue. Consider I have a parent class A and a derived class B from A and a derived Class C from A again.

Let's say, class B & C both override method seed() from class A. Also clases A & B & C all implement a static function seedAll().

My question is: How do I call seedAll for B & C from the seedAll function of A without explicitly specifying the class B & C, so that in the future, should I have a class D similar to class B & C, I need not modify the seedAll function of class A to call the seedAll functions of classes B, C & D.

abstract class A
{
    public static function seedAll() {
        //How do I call seedAll of all the derived classes of Seeder?
    }

    abstract protected function seed();
    // // Common method
    // public function printOut() {
    //     print $this->getValue() . "\n";
    // }
}

class B extends A
{
    private $name;

    function __construct($name) {
        $this->name = $name;
    }

    public static function seedAll() {
        $seeder1 = new B("name1");
        $seeder1 = new B("name2");
    }

    function seed() {
        echo $this->name;
    }
}


  class C extends A
    {
        private $name;

        function __construct($name) {
            $this->name = $name;
        }

        public static function seedAll() {
            $seeder1 = new C("name1");
            $seeder1 = new C("name2");
        }

        function seed() {
            echo $this->name;
        }
    }
Hirvesh
  • 7,636
  • 15
  • 59
  • 72
  • 1
    It shouldn't be a static method if it depends on which object you're calling it from. – Barmar Jun 16 '17 at 16:05
  • Which one shouldn't be a static method, the static method of the base class or that of the derived classes? Can you be more clear? – Hirvesh Jun 16 '17 at 16:07
  • 1
    How is the `seed()` method related to this? Can you show the code, and what result you're trying to get? – Barmar Jun 16 '17 at 16:09
  • @Barmar, I updated the description and put the code. I would appreciate if you could help me re. this. – Hirvesh Jun 16 '17 at 16:12
  • You haven't shown how you're using the class and what result you're trying to get. – Barmar Jun 16 '17 at 16:14
  • 1
    There's no OOP way to do this. – apokryfos Jun 16 '17 at 16:15
  • This is exactly what I don't know how to do. I'm trying to get the seedAll function of A to call the seedAll function of B and seedAll function of C without explicitly specifying class B or C. I want to know if there is a way to know all derived classes of A and call their respective seedAll functions. – Hirvesh Jun 16 '17 at 16:16
  • You need to use introspection to get all the subclasses. – Barmar Jun 16 '17 at 16:17
  • @Barmar, since you're so exigent on having code samples, could you please share a code sample on how I would achieve this using introspection? – Hirvesh Jun 16 '17 at 16:20
  • "How do I call seedAll for B & C from the seedAll function of A?" Don't: base classes must know nothing of their derived classes. – bishop Jun 16 '17 at 16:26

1 Answers1

2

See how to obtain all subclasses of a class in php for how to get all the subclasses. You can then loop over them and call their seedAll() methods.

abstract class A
{
    public static function seedAll() {
        $subclasses = getSubclassesOf("A");
        foreach ($subclasses as $class) {
            $class::seedAll();
        }
    }

    abstract protected function seed();
    // // Common method
    // public function printOut() {
    //     print $this->getValue() . "\n";
    // }
}

getSubclassesOf function:

function getSubclassesOf($parent) {
    $result = array();
    foreach (get_declared_classes() as $class) {
        if (is_subclass_of($class, $parent))
            $result[] = $class;
    }
    return $result;
}
Hirvesh
  • 7,636
  • 15
  • 59
  • 72
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Some caveats: (1) Use of autoloading means some classes may not be known when this function is called (2) if OP forgets to implement `seedAll` in any subclass then this will cause an infinite recursion. However OP asked for it so it's a "buyer beware" kind of situation. – apokryfos Jun 16 '17 at 16:25
  • @Barmar I would mark your answer as accepted, but I find that the getSubclassesOf function is not part of the PHP standard library. – Hirvesh Jun 16 '17 at 16:29
  • Right. It's in the question I linked to. – Barmar Jun 16 '17 at 16:32
  • @Barmar thank you very much for all your help and time! – Hirvesh Jun 16 '17 at 16:38