Jeśli chcesz zadzwonić do konstruktora najbliższego przodka, możesz przetoczyć się przez przodków z class_parents i sprawdzić z method_exists, jeśli ma konstruktor. Jeśli tak, zadzwoń do konstruktora; jeśli nie, kontynuuj wyszukiwanie z następnym najbliższym przodkiem.Nie tylko zapobiega nadrzędnymi konstruktora rodzica, ale także od innych przodków (w przypadku, gdy rodzic nie posiada konstruktora):
class Queue extends SplQueue {
public function __construct() {
echo 'before';
// loops through all ancestors
foreach(class_parents($this) as $ancestor) {
// check if constructor has been defined
if(method_exists($ancestor, "__construct")) {
// execute constructor of ancestor
eval($ancestor."::__construct();");
// exit loop if constructor is defined
// this avoids calling the same constructor twice
// e.g. when the parent's constructor already
// calls the grandparent's constructor
break;
}
}
echo 'I have made it after the parent constructor call';
}
}
Do ponownego użycia kodu, można również napisać ten kod w funkcji, która zwraca kod PHP być eval
ed:
// define function to be used within various classes
function get_parent_construct($obj) {
// loop through all ancestors
foreach(class_parents($obj) as $ancestor) {
// check if constructor has been defined
if(method_exists($ancestor, "__construct")) {
// return PHP code (call of ancestor's constructor)
// this will automatically break the loop
return $ancestor."::__construct();";
}
}
}
class Queue extends SplQueue {
public function __construct() {
echo 'before';
// execute the string returned by the function
// eval doesn't throw errors if nothing is returned
eval(get_parent_construct($this));
echo 'I have made it after the parent constructor call';
}
}
// another class to show code reuse
class AnotherChildClass extends AnotherParentClass {
public function __construct() {
eval(get_parent_construct($this));
}
}
Tak z ciekawości, dlaczego rozszerzenie klasy kolejki? Co musisz zrobić, aby dekoracja nie była? – ircmaxell