[英]Multiple constructor parameter injection in php-di
我正在嘗試使用PHP-DI ,但並沒有成功。 在我的簡單scanario中,在Wordpress Theme中的控制器需要在構造函數中注入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:object(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:object(CategoryService)[3006] C:\\ xampp \\ apps \\ wordpress \\ htdocs \\ wp-content \\ themes \\ chester-nanalab \\ mvc \\ controllers \\ index_controller.php:16:object(CategoryService)[ 3006]
所以:
致命錯誤:未捕獲錯誤:在第19行上,調用C:\\ xampp \\ apps \\ wordpress \\ htdocs \\ wp-content \\ themes \\ chester-nanalab \\ mvc \\ controllers \\ index_controller.php中未定義方法CategoryService :: GetLastPostByCategory()
但是,如果我更改分配順序:
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:object(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:object(PostService)[3005] C:\\ xampp \\ apps \\ wordpress \\ htdocs \\ wp-content \\ themes \\ chester-nanalab \\ mvc \\ controllers \\ index_controller.php:18:object(PostService)[ 3005]
(╯°□°)╯︵┻━┻ ?它有效!有人可以向我解釋發生了什么嗎?
提前致謝。
問題是您將對象屬性稱為$this->$property
。 可以像$this->property
這樣訪問$this->property
但是定義了VISIBILITY $property;
因此,您應該將代碼更改為此
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);
}
}
對於parent
它是不同的,因為您使用的是static
訪問器(您沒有將屬性設置為靜態的,但是這是這樣做的方法) parent::$property
。
記住,對於任何魔術方法 ,它都是小寫的__construct
。
您可以在此處獲得有關類和對象的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.