Примечание: автоматическая загрузка PSR-4 была настроена в композиторе.json и композитор du был выполнен.
Код: Выделить всё
require __DIR__.'/../vendor/autoload.php';
use App\Service\FooBar;
use App\Service\Bar;
$container = new \League\Container\Container();
$container->add('foobar', FooBar::class)->addArgument(Bar::class);
$container->add('bar', Bar::class);
$foo = $container->get('foobar'); //
Uncaught TypeError: App\Service\FooBar::__construct() : Аргумент №1 ($bar) должен иметь тип App\Service\Bar, заданную строку
Определенные классы
Код: Выделить всё
// file: FooBar.php
namespace App\Service;
class FooBar {
private $bar;
/**
* @param \App\Service\Bar $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 App\Service;
class Bar {
public function __construct() {
var_dump('Constructor of ' . __CLASS__ . ' called!');
}
}
Код: Выделить всё
$container->add(Bar::class);
Подробнее здесь: https://stackoverflow.com/questions/786 ... ives-error