I am trying the example code from the container of thephpleague found at : https://container.thephpleague.com/4.x/ and what I have is pretty basic IMHO.
Note: PSR-4 auto loading has been configured in composer.json and composer du
has been executed.
require __DIR__.'/../vendor/autoload.php';
use AppServiceFooBar;
use AppServiceBar;
$container = new LeagueContainerContainer();
$container->add('foobar', FooBar::class)->addArgument(Bar::class);
$container->add('bar', Bar::class);
$foo = $container->get('foobar'); //this line gives an error Uncaught TypeError: AppServiceFooBar::__construct(): Argument #1 ($bar) must be of type AppServiceBar, string given
The classes defined
// file: FooBar.php
namespace AppService;
class FooBar {
private $bar;
/**
* @param AppServiceBar $bar
*/
public function __construct(Bar $bar) {
$this->bar = $bar;
var_dump('Constructor of '. __CLASS__ . ' called!');
}
public function getBar() {
return $this->bar;
}
}
//file: Bar.php
namespace AppService;
class Bar {
public function __construct() {
var_dump('Constructor of ' . __CLASS__ . ' called!');
}
}
If I register the following to the container I don’t get an error.
$container->add(Bar::class);
I am trying to understand what is the problem with the first snippet which comes from the example code.