相同的命名空间找不到类的构造函数

时间:2017-11-04 00:08:48

标签: php phpunit slim

我正在尝试实例化A类的对象,该对象与当前类C的命名空间相同,但是我失败了。

这两个类都在命名空间App \ Models中。

这是A.php的代码:

namespace App\Models;

class A implements B
{
    private $url;

    public function __construct($url = "")
    {
        $this->url = $url;
    }
}

这是C.php的代码:

namespace App\Models;
require_once 'A.php';

class C
{
    private $url;

    ...some functions...

    public function getC()
    {
        $test = A($this->url);
        return $test;
    }

    ...other functions
}

我得到了

Error: Call to undefined function App\Models\A() 
在phpunit中,我无法理解我做错了什么。

我正在使用PHP 7.0.24

1 个答案:

答案 0 :(得分:1)

通过调用A(),您将A()作为一项功能调用。您似乎忘记了new

class C
{
    private $url;

    ...some functions...

    public function getC()
    {
        $test = new A($this->url);
        return $test;
    }

    ...other functions
}

你做了一个简单的错字 - 它发生在我们最好的人身上。

相关问题