类实例化在类声明之上与之下


Class instantiation above vs below class declaration

我有一个名为UserController的类,它是通过对UserController.php的ajax调用在自己的声明之上实例化的。它按预期工作。

<?php
new UserController();
class UserController{
  //constructor
  //methods
}
?>

现在,我想为我的所有控制器提供一些通用功能,并通过扩展baseController来实现这一点。

<?php
//include BaseController
new UserController();
class UserController extends BaseController{
  //constructor
  //methods
}
?>

然而,当尝试实例化时,我得到了以下消息:

致命错误:在C:''examplep''htdocs''project''controllers''UserController.php中找不到类"UserController"

如果我将实例化移动到声明之后。一切又好起来了。

<?php
//include BaseController
class UserController extends BaseController{
  //constructor
  //methods
}
new UserController();
?>

有人能解释一下吗?

为什么在这种情况下,实例化UserController类需要在其声明之后完成,而如果不扩展另一个类,则可以在声明之前完成?

来自文档:

除非使用自动加载,否则必须在使用类之前定义它们。如果一个类扩展了另一个类,则父类必须在子类结构之前声明。此规则适用于继承其他类和接口的类。

此处参考

并且CCD_ 1没有自动加载。。。所以有问题