2017-11-04 177 views
1

我想实例化类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

+3

我猜你忘了'new'? '$ test = new A($ this-> url);'?你将它作为一个函数调用按原样调用。我们可以将这个问题作为印刷错误/简单的错字来解决吗? – HPierce

+0

请正式回答,以便我可以注意到它的答案。我为此挣扎了4个小时。我以前从来没有觉得这很愚蠢。非常感谢。 –

回答

1

通过调用A()你调用A()的功能。您似乎忘记一个new

class C 
{ 
    private $url; 

    ...some functions... 

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

    ...other functions 
} 

你做了一个简单的拼写错误 - 它发生在我们最好的。

相关问题