Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP致命错误:未找到接口,使用名称空间的工厂模式_Php_Design Patterns_Interface - Fatal编程技术网

PHP致命错误:未找到接口,使用名称空间的工厂模式

PHP致命错误:未找到接口,使用名称空间的工厂模式,php,design-patterns,interface,Php,Design Patterns,Interface,文件1-/Users/jitendraojha/www/DesignPatternsWithPhpLanguage/FactoryPattern/FactoryClassPattern/UserInterface.php <?php namespace DesignPatternsWithPhpLanguage\FactoryPattern\FactoryClassPattern; interface UserInterface { function setFirstName

文件1-
/Users/jitendraojha/www/DesignPatternsWithPhpLanguage/FactoryPattern/FactoryClassPattern/UserInterface.php

<?php

namespace DesignPatternsWithPhpLanguage\FactoryPattern\FactoryClassPattern;

interface UserInterface
{

    function setFirstName($firstName);
    function getFirstName();
}

?>
<?php

namespace DesignPatternsWithPhpLanguage\FactoryPattern\FactoryClassPattern;


class User implements UserInterface
{

    private $firstName = null;

    public function __construct($params) {   }

    public function setFirstName($firstName)
    {

        $this->firstName = $firstName;
    }

    public function getFirstName()
    {

        return $this->firstName;
    }
}

?>
问题

php FactoryPattern/FactoryClassPattern/UserInterface.php
-运行良好

php FactoryPattern/FactoryClassPattern/User.php
-给出以下错误:php致命错误:在第7行的/Users/jitendraojha/www/DesignPatternsWithPhpLanguage/FactoryPattern/FactoryClassPattern/User.php中找不到接口“DesignPatternsWithPhpLanguage\FactoryPattern\FactoryClassPattern\UserInterface”


我使用了
用户界面添加到文件2中,但没有解决方案。

您需要它包含的所有内容,但最好使用自动加载器

有关包含的快速测试,请参见下面的示例,假设两个文件位于同一目录中

namespace DesignPatternsWithPhpLanguage\FactoryPattern\FactoryClassPattern;

include('UserInterface.php');

class User implements UserInterface
{

    private $firstName = null;

    public function __construct($params) {   }

    public function setFirstName($firstName)
    {

       $this->firstName = $firstName;
    }

    public function getFirstName()
    {

       return $this->firstName;
     }
}


您需要在User.php文件之前包含UserInterface.php文件。

问题在于
User.php
,您没有将其添加到问题中。你也可以添加那个文件吗?@Kenney-见文件-2-那就是
User.php
。噢,对不起不知道为什么我没有得到那个…嘿,那个工作,但我如何得到
使用用户界面在文件2中工作?明白了,必须实现一个自动加载方法,这是依赖于框架的缺点:(
// Quick test will - output ===> John
$user = new User(null);
$user->setFirstName('John');
echo $user->getFirstName();