类扩展时连接超时


Connection timed out On Class Extend

我是PHP的新手,所以我确信我做错了什么,但我的代码很简单,我似乎看不出是什么导致了超时错误。

index.php

include_once ("./inc/controller/controller.php");
$controller = new controller();
$controller->index();

controller.php

include_once('./inc/class/class.common.php');
    class controller {
        public function __construct(){
            $this->common = new Common; // This Line Causes the Error
            echo "Everything Loaded"; // This only executes when above line is gone
        }
        public function index(){
            die("done");    
        }
    }

class.common.php

class Common extends controller{
   // I don't even have code in here yet
}

查看我的日志,我看到错误

Class 'controller' not found in /inc/class/class.common.php 

为什么它找不到控制器。

当您扩展一个类而不定义构造函数时,它将从父类继承。因此,当您在控制器内创建Common实例时,它本身将尝试创建Common的实例。然后,该实例将尝试在一个无限循环中创建一个,依此类推。

您可以简单地重写Common中的构造函数。一个空的就可以了:

class Common extends controller{
   public function __construct() {
   }
}

但是,想要一个已经在扩展的对象的单独实例是很奇怪的。