如何包括扩展接口的php类 [英] How to include php class that extends an interface

查看:53
本文介绍了如何包括扩展接口的php类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个必须使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆