While using Eloquent as a standalone library, i decided to introduce some Auto Wiring for my Eloquent Models, to automatically provide any dependencies.
I did this using ReflectionClass
and PHP-DI/Invoker's internal workings like the below snippet. This gets the arguments i needed to create a new class of the Model, if it required anything beyond the Attributes automatically.
$resolver = $invoker->getParameterResolver();
$reflectionClass = new \ReflectionClass($modelName);
$reflectionMethod = $reflectionClass->getConstructor();
$parameters = $resolver->getParameters($reflectionMethod, [], []);
$model = $reflectionClass->newInstanceArgs($parameters);
The problem with this method is that, for whatever insane reason that php has going on, the Model constructor is never called. I confirmed this by calling die
in both the Model class from the Eloquent package, and my Custom Model (in this case, User) __construct
function. Only my custom Model stopped the execution of the script.
I can get it working if i simply do the following
public function __construct(array $attributes = []) {
parent::__construct($attributes)
}
But ofc, this is obviously less than optimal since it would be required in every single model i create and use via this method.
So long question short: What the hell is doing on, why is the __construct
function on the parent class not being called when creating a new instance of it via ReflectionClass
Aucun commentaire:
Enregistrer un commentaire