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



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

这两个类都位于命名空间应用\模型中。

这是 A 的代码.php:

namespace AppModels;
class A implements B
{
    private $url;
    public function __construct($url = "")
    {
        $this->url = $url;
    }
}

这是 C 的代码.php:

namespace AppModels;
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 AppModelsA() 

在phpunit中,我不明白我做错了什么。

我正在使用 PHP 7.0.24

通过调用A(),您将A()作为函数调用。看起来你忘记了new

class C
{
    private $url;
    ...some functions...
    public function getC()
    {
        $test = new A($this->url);
        return $test;
    }
    ...other functions
}

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

最新更新