我在SO上发现了一个问题,该问题以我不理解的方式使用了php"use"关键字。以下是代码片段:
<?php
namespace My;
use Zend'ServiceManager'ServiceLocatorAwareInterface;
use Zend'ServiceManager'ServiceLocatorAwareTrait;
class MyClass implements ServiceLocatorAwareInterface{
use ServiceLocatorAwareTrait;
public function doSomething(){
$sl = $this->getServiceLocator();
$logger = $sl->get( 'My'CusomLogger')
}
}
// later somewhere else
$mine = $serviceManager->get( 'My'MyClass' );
//$mine now has the serviceManager with in.
现在我尝试了这个没有use ServiceLocatorAwareTrait;
行的代码,但它不起作用。既然页面顶部已经有use Zend'ServiceManager'ServiceLocatorAwareTrait;
,为什么还要做use ServiceLocatorAwareTrait;
呢。这是用默认值初始化接口所需方法的特殊方法吗?
类声明之外的"use"正在导入其他命名空间-类/接口。
Class声明中的一个是将Trait导入到Class中,这样类就可以访问Trait的行为。
命名空间-http://php.net/manual/en/language.namespaces.php特征-http://php.net/manual/en/language.oop5.traits.php