Я пытаюсь использовать PHP-DI, но мне это не совсем удалось. В моем простом сканировании контроллеру в теме Wordpress нужны PostService и CategoryService, внедренные в конструктор:
class IndexController extends ChesterBaseController {
private $_postservice;
private $_categoryService;
public function __construct(PostService $postservice, CategoryService $categoryService){
var_dump($postservice);
var_dump($categoryService);
parent::__CONSTRUCT();
$this->$_categoryService = $categoryService;
$this->$_postservice = $postservice;
var_dump($this->$_postservice);
var_dump($this->$_categoryService);
}
public function Index(){
$firstRowPost = $this->$_postservice->GetLastPostByCategory('video');
// ...
echo $this->renderPage('index', $vm);
}
}
Это моя точка входа для контейнера в Index.php:
require_once 'vendor/autoload.php';
require_once dirname(__FILE__).'/mvc/controllers/index_controller.php';
require_once dirname(__FILE__).'/mvc/services/categoryService.php';
require_once dirname(__FILE__).'/mvc/services/postService.php';
use DI\Container;
use DI\ContainerBuilder;
$builder = new DI\ContainerBuilder();
$builder->addDefinitions(['config.php']);
$container = $builder->build();
$indexController = $container->get('IndexController');
$indexController->Index();
И «config.php», который содержит определения:
return [
'PostService' => \DI\object('PostService'),
'CategoryService' => \DI\object('CategoryService'),
'IndexController' => \DI\object()->constructor(DI\get('PostService'),DI\get('CategoryService'))
];
Это результат выполнения:
C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:10: объект(PostService)[3005] C:\xampp\apps\wordpress\htdocs\wp -content\themes\chester-nanalab\mvc\controllers\index_controller.php:11: object(CategoryService)[3006] C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers \index_controller.php:15: объект(CategoryService)[3006] C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:16: объект(CategoryService)[ 3006]
так что:
Неустранимая ошибка: Uncaught Error: вызов неопределенного метода CategoryService::GetLastPostByCategory() в C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php в строке 19
но если я изменю порядок заданий:
public function __construct(PostService $postservice,CategoryService $categoryService){
var_dump($postservice);
var_dump($categoryService);
parent::__CONSTRUCT();
$this->$_categoryService = $categoryService;
$this->$_postservice = $postservice;
var_dump($this->$_postservice);
var_dump($this->$_categoryService);
}
Я могу читать:
C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:10: объект(PostService)[3005] C:\xampp\apps\wordpress\htdocs\wp -content\themes\chester-nanalab\mvc\controllers\index_controller.php:11: object(CategoryService)[3006] C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers \index_controller.php:17: объект(PostService)[3005] C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-nanalab\mvc\controllers\index_controller.php:18: объект(PostService)[ 3005]
(╯°□°)╯︵ ┻━┻? Это работает! Кто-нибудь может мне объяснить что случилось?
Заранее спасибо.
$this->$_categoryService = $categoryService;
и$this->$_postservice = $postservice;
, вы должны звонить$this->_categoryService
и$this->_postservice
- person matiaslauriti   schedule 08.10.2017