如何包含扩展接口的 php class
How to include php class that extends an interface
我正在开发一个必须使用 CommandService 处理数据的套接字侦听器 class。
CommandService.php
<?php
namespace Application\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
class CommandService implements ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;
}
custom.php
<?php
include('../module/Application/src/Application/Service/CommandService.php');
?>
当我在控制台运行
php custom.php
我收到错误:
致命错误:在第 7 行的 C:\wamp\www\nutirent\module\Application\src\Application\Service\CommandService.php 中找不到接口 'Zend\ServiceManager\ServiceLocatorAwareInterface'
所以,如果有人能给我一些建议,我会很高兴我怎样才能得到这个 运行ning 而不使 custom.php 成为 class。
如果您没有使用任何自动加载系统(即作曲家的自动加载器),那么您有责任加载所有依赖项。
所以在这种情况下,仅加载 CommandService 是不够的,您还需要加载(包括)Zend\ServiceManager\ServiceLocatorAwareInterface以及所有其他依赖项:)
所以我强烈建议考虑使用 autoloader ;)
阿里的回答完全正确,我只想说说ServiceLocatorAwareInterface :
前段时间,我在我的服务中是 ServiceLocatorAwareInterface
的忠实粉丝。现在我不太确定了。你必须考虑这个:
Have the ServiceLocator in your Services make them trashy.
因为服务定位器就是这样,所以您不能再说您的 class 依赖什么,因为可能是一切。
你应该改用依赖注入,并用工厂加载你的服务需要的任何东西,工厂是你唯一可以使用 ServiceLocator 的地方,这是它所属的地方。
Ocramius 说的比我现在想说的要好,所以我 link 在这里他的工作:
这启发了我。希望你也是。
我正在开发一个必须使用 CommandService 处理数据的套接字侦听器 class。
CommandService.php
<?php
namespace Application\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
class CommandService implements ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;
}
custom.php
<?php
include('../module/Application/src/Application/Service/CommandService.php');
?>
当我在控制台运行
php custom.php
我收到错误: 致命错误:在第 7 行的 C:\wamp\www\nutirent\module\Application\src\Application\Service\CommandService.php 中找不到接口 'Zend\ServiceManager\ServiceLocatorAwareInterface'
所以,如果有人能给我一些建议,我会很高兴我怎样才能得到这个 运行ning 而不使 custom.php 成为 class。
如果您没有使用任何自动加载系统(即作曲家的自动加载器),那么您有责任加载所有依赖项。
所以在这种情况下,仅加载 CommandService 是不够的,您还需要加载(包括)Zend\ServiceManager\ServiceLocatorAwareInterface以及所有其他依赖项:)
所以我强烈建议考虑使用 autoloader ;)
阿里的回答完全正确,我只想说说ServiceLocatorAwareInterface :
前段时间,我在我的服务中是 ServiceLocatorAwareInterface
的忠实粉丝。现在我不太确定了。你必须考虑这个:
Have the ServiceLocator in your Services make them trashy.
因为服务定位器就是这样,所以您不能再说您的 class 依赖什么,因为可能是一切。 你应该改用依赖注入,并用工厂加载你的服务需要的任何东西,工厂是你唯一可以使用 ServiceLocator 的地方,这是它所属的地方。
Ocramius 说的比我现在想说的要好,所以我 link 在这里他的工作:
这启发了我。希望你也是。