мануальный PHP: $container = new Container(); $application = new Application($container); class Application { private $container; private $router; public function __construct(Container $container) { $this->container = $container; $this->router = $this->container->get(Router::class); } } автовайринг PHP: $container = new Container(); $application = Application::create($container); class Application { private static $container; private $router; public function __construct(Router $router) { $this->router = $router; } public static function create(Container $container): self { self::$container = $container; return self::$container->get(self::class); } } Какая принципиальная разница?
вообще ничего не понятно --- Добавлено --- я тупой наверно --- Добавлено --- PHP: class Application { public function __construct(private readonly Container $container) { //$this->container = $container; /* ... */ } } --- Добавлено --- надо кстати у себя прописать "чтение" --- Добавлено --- во, теперь зае6ись PHP: <?php declare ( strict_types = 1 ); namespace Nouvu\Framework\Foundation; use Psr\Container\ContainerInterface; class Application { public function __construct ( private readonly ContainerInterface $ContainerInterface, array $tools ) { foreach ( [ ...$tools, \App :: class => ( fn( ContainerInterface $ContainerInterface ): self => $this ) ] AS $name => $packaging ) { $this -> ContainerInterface -> set( $name, $packaging ); } } public function __set( string $name, callable $value ): void { $this -> ContainerInterface -> set( $name, $packaging ); } public function __get( string $name ): mixed { return $this -> ContainerInterface -> get( str_replace ( '.', '\\', $name ) ); } }
@MouseZver мне в смысле статика мозолит глаза, если у меня в контейнере предусмотрен автовайринг, так может вообще делать сразу PHP: $container = new Container(); $application = $container->get(Application::class);
вот и я размышляю: зачем мне передавать контейнер либо возвращать статикой сам себя объект, какая-то карусель