Consider this simple class:
<?php declare(strict_types=1);
class Blog
{
private $user;
public function __construct(string $user)
{
$this->user = $user;
}
public function post ()
{
return 'New post by '.$this->user;
}
}
Using basic dependency injection works fine:
$blog = new Blog('Bob');
echo $blog->post();
// no errors, works as expected
$blog = new Blog(7);
echo $blog->post();
// fatal errors with TypeError, works as expected
But when using a dependency container like league\container the strict_types declaration is ignored?
$container = new League\Container\Container;
$container->add(Blog::class)->addArgument(7);
$foo = $container->get(Blog::class);
echo $foo->post();
// I passed `7` in the parameter, it doesn't work as expected
// because it should've fatal error with TypeError because `7` is not a string
A quick view of the src
folder of league\container
reveals it's also using declare(strict_types=1);
in all of its files. That said, I'm not sure if it's possible for the container to detect the parameter types of the class its initiating. If it's not possible then using declare(strict_types=1);
is useless in this case?