I've been looking for help for a while, have two methods which work under php 7.4 but no under php 8, its because in php8 ReflectionParameter::getType() replace getClass(), but when i make the modification it doesn't work. how to update the code and make it work please ?
All code and additional information
the problematic code :
public function register(string $id): self
{
$reflectionClass = new ReflectionClass($id);
if ($reflectionClass->isInterface()) {
$this->register($this->aliases[$id]);
$this->definitions[$id] = &$this->definitions[$this->aliases[$id]];
return $this;
}
$dependencies = [];
if (null !== $reflectionClass->getConstructor()) {
var_dump($reflectionClass->getConstructor());
$dependencies = array_map(
fn (ReflectionParameter $parameter) => $this->getDefinition($parameter->getClass()->getName()),
array_filter(
$reflectionClass->getConstructor()->getParameters(),
fn (ReflectionParameter $parameter) => $parameter->getClass()
)
);
}
$aliases = array_filter($this->aliases, fn (string $alias) => $id === $alias);
$this->definitions[$id] = new Definition($id, true, $aliases, $dependencies);
return $this;
}
public function make(ContainerInterface $container): object
{
$constructor = $this->class->getConstructor();
if (null === $constructor) {
return $this->class->newInstance();
}
$parameters = $constructor->getParameters();
return $this->class->newInstanceArgs(
array_map(function (ReflectionParameter $param) use ($container) {
if ($param->getClass() === null) {
return $container->getParameter($param->getName());
}
return $container->get($param->getClass()->getName());
}, $parameters)
);
}
Aucun commentaire:
Enregistrer un commentaire