如何包括扩展接口的php类 [英] How to include php class that extends an interface
问题描述
我正在开发一个必须使用CommandService类处理数据的套接字侦听器。
CommandService.php
<?php
命名空间Application\Service;
使用Zend\ServiceManager\ServiceLocatorAwareInterface;
使用Zend\ServiceManager\ServiceLocatorAwareTrait;
类CommandService实现ServiceLocatorAwareInterface
{
使用ServiceLocatorAwareTrait;
}
custom.php
<?php
include('../ module / Application / src / Application / Service / CommandService.php');
?>
当我在控制台上运行时
php custom.php
我收到错误:
致命错误:在C:\wamp\www\nutirent\module\Application\src\Application\Service\CommandService.php中找不到接口'Zend\ServiceManager\ServiceLocatorAwareInterface'在第7行上。
所以,如果有人可以给我一些建议,而又不将custom.php作为类运行该如何运行,那我就不好了。
如果您不使用任何自动加载系统(例如,作曲家的自动加载器),则您有责任加载所有依赖项。 / p>
因此,在这种情况下,仅加载 CommandService 是不够的,您还需要加载(包括) Zend\ServiceManager\ ServiceLocatorAwareInterface 以及所有其他依赖项:)
因此,我强烈建议您考虑使用自动加载器;)
I'm developing a socket listener that has to process data with 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');
?>
And when I run on console
php custom.php
I get error: Fatal error: Interface 'Zend\ServiceManager\ServiceLocatorAwareInterface' not found in C:\wamp\www\nutirent\module\Application\src\Application\Service\CommandService.php on line 7
So I't would be great if someone can give me some advice how can I get this running without making custom.php as class.
If you are not using any automated loading system (i.e. composer's autoloader) then its your responsibility to load all the dependencies.
So in this case just loading the CommandService is not enough and you also need to load (include) Zend\ServiceManager\ServiceLocatorAwareInterface as well as all the other dependencies :)
So I would strongly suggest to consider using an autoloader ;)
这篇关于如何包括扩展接口的php类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!