Return values through a class derived from the same parent

2

I have classes 1, 2a and 2b, where 1 is the main class, while 2a and 2b extends class 1. Is it possible through class 2a to access values from class 2b directly or through the parent class?

Basic example:

class system{
}

class modulos extends system{

    function __construct(){
        $exemple = controllers::exemple();
    }
}

class controllers extends system{

    public function exemple(){
        return true;
    }
}

$system = new system();
    
asked by anonymous 21.03.2014 / 16:13

2 answers

1

This way you did is impossible. When modulos and controllers inherit from system each class follows a different path and the two do not relate.

If there is a common method that will be used by all classes that inherit from system , then by logic who should contain this method is system .

class system{
    protected function exemple() {
        return true;
    }
}

class modulos extends system{

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

class controllers extends system{}

$system = new system();

There are other ways to work around this, but all of them including the one I gave above will depend on how you want the relationship between your classes to be.

    
20.04.2014 / 23:17
0

Hello

Since class 2b (controllers) is a dependency of class 2a (modules), I believe that the 2a constructor should receive a 2b reference. p>

I would do it this way:

class modulos extends system{

  function __construct($controllers){
      $exemple = $controllers::exemple();
  }
}
    
21.03.2014 / 18:27