开始使用 Laravel 后,我在 php 中遇到了名为“命名空间”的主题。在尝试理解命名空间时,我发现要在命名空间下扩展类,我需要在当前页面中包含该类。像下面这样:
目录'..\teacher\Teacher.php'
namespace Teacher;
class Teacher{
public $headTeacher='mr X';
}
要扩展我需要的 calss 包括 该页面以及使用 命名空间
目录'..\studnet\student.php'
use \Teacher\Teacher; //use the namespace
include('../teacher/Teacher.php'); // include the page
class mathTeacher extends Teacher{
public function headTeacherName(){
echo $this->headTeacher;
}
}
$student=new mathTeacher();
$student->headTeacherName();
我想知道 Laravel 如何只使用命名空间来包含类。就像我创建一个名为“userController”的 Controller 一样。页面结构为
namespace App\Http\Controllers;
class userController extends Controller{
}
他们从不包含包含 的 php 页面。 “ Controller ”类(class)。但他们能够以某种方式扩展它。我也可以使用
"View" ,"Auth"
只需使用 use View
或 use Auth
命令。它是如何完成的?如何使用我提供的代码实现相同的功能?提前致谢。
最佳答案
Laravel 使用 composer.php 自动加载类。自动加载目录中的所有类都将被预加载。所以你可以只使用命名空间并在应用程序中的任何地方使用。
Learn more about composer ,composer 配置可以在应用程序的根路径中的 composer.json 中找到
关于php - laravel 如何在不包含类的情况下扩展类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318575/