2017-11-04 174 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 
} 

你做了一個簡單的拼寫錯誤 - 它發生在我們最好的。

相關問題