PHP: Hypertext Preprocessor (original) (raw)
`We can make a "Automatic dependenci injector" in classes when her constructors depends other classes (with type hint).
this−>dep1=this->dep1 = this−>dep1=dependence1; this−>dep2=this->dep2 = this−>dep2=dependence2; } } // Automatic dependence injection (CLASSES)$constructor = new ReflectionMethod(myClass::class, '__construct'); parameters=parameters = parameters=constructor->getParameters();$dependences = []; foreach ($parameters as $parameter) { dependenceClass=(string)dependenceClass = (string) dependenceClass=(string)parameter->getType(); dependences[]=newdependences[] = new dependences[]=newdependenceClass(); }$instance = new myClass(...$dependences); var_dump($instance);?>Results in:
object(myClass)#6 (2) {
["dep1":"myClass":private]=>
object(Dependence1)#4 (0) {
}
["dep2":"myClass":private]=>
object(Dependence2)#5 (0) {
}
}
`