如何包含扩展接口的php类


How to include php class that extends an interface

我正在开发一个套接字侦听器,它必须使用CommandService类处理数据。

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"

因此,如果有人能给我一些建议,我就不会太好了。我如何在不将custom.php作为类的情况下运行它。

如果您没有使用任何自动加载系统(即composer的自动加载器),则您有责任加载所有依赖项。

因此,在这种情况下,仅加载CommandService是不够的,您还需要加载(包括)Zend''ServiceManager''ServiceLocatorAwareInterface以及所有其他依赖项:)

因此,我强烈建议考虑使用自动加载器;)

Ali的回答完全正确,我只想谈谈ServiceLocatorAwareInterface:

前段时间,我是ServiceLocatorAwareInterface的忠实粉丝。现在我不太确定。你必须考虑一下:

让你的服务中的ServiceLocator让它们变得垃圾。

因为服务定位器就是它,所以你不能再说你的类依赖什么了,因为可能所有的东西都依赖。你应该使用Dependency注入,并用工厂加载你的服务需要的任何东西,工厂是你唯一可以使用ServiceLocator的地方,这是它所属的地方。

Ocramius说这比我现在想说的要好,所以我把他的工作链接到这里:

Ocramius Zf2最佳实践

这激励了我,我也希望你。