代码之家  ›  专栏  ›  技术社区  ›  Panos Filianos

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

  •  1
  • Panos Filianos  · 技术社区  · 8 年前

    我试图实例化一个类a的对象,该类与我当前的类C具有相同的命名空间,但我失败了。

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

    这是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 回复  |  直到 8 年前
        1
  •  1
  •   HPierce    8 年前

    通过呼叫 A() 你正在调用 A() 作为一种功能。看起来你忘了一个 new :

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

    你犯了一个简单的拼写错误——这发生在我们中最好的人身上。

    推荐文章