[英]Class Not Found in PHP Laravel 6.x when another class extends
I always get the class not found exception in php laravel 6 when i create a class and extend a parent class named A which is located in the same directory.
但是,位於同一目錄中的另一個子 class 可以成功擴展 class A。
此外,由於 class 在 another.php 文件中未發現異常,我也無法實例化 class A。
請幫助我。 預先感謝。
父 class:myContext
<?php
namespace config\models;
class myContext {
public static $conn;
...
}
Class myUser:擴展名很好。
<?php
namespace config\models;
class myUser extends myContext {
private $name;
...
}
Class oauth:擴展返回 myContext class 未找到。
<?php
namespace config\models;
class oauth extends myContext {
private $user;
}
實例化 class - 返回未找到 class。
<?php
use config\models\myContext as context;
$cont = new context();
在 composer.json 文件中,
"autoload": {
"psr-4": {
"App\\": "app/",
"config\\models\\": "config/models"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
在那之后composer dump-autoload
。
要使提供的示例代碼正常工作,您需要使用require_once
<?php
require_once('models/myContext.php');
use app\config\models\myContext as context;
$test = new context();
對 SO 的搜索將我帶到了這個來源
你有沒有為composer.json
中的config/
文件夾添加自動加載。 Laravel 僅默認自動加載app/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.