檔案結構:
- 控制器/Foo.php
- 模型/Bar.php
- 模型/函式.php
在 Foo.php 中,使用 require_once() 我可以使用 Functions.php 中的函式,但我不能實體化 Bar 類?它給出了錯誤"Undefined type 'App\Controllers\Bar'."。我不確定我做錯了什么。
如果有幫助,這是在 Codeigniter 專案中,因此 Bar 擴展了資料庫。
Foo.php
require (realpath(dirname(__FILE__)) . '\..\Models\Functions.php');
require (realpath(dirname(__FILE__)) . '\..\Models\Bar.php');
class Foo extends Controller {
public function get_data($input) {
$oBar = new Bar() // this is where the error occurs
}
}
酒吧.php
<?php
class Bar extends Database {
public function do_something() {
echo 'something else';
}
}
函式.php
<?php
function say_hi() {
echo 'hi';
}
uj5u.com熱心網友回復:
也許您正在使用名稱空間,因為錯誤說明了它'App\Controllers\Bar'。
因此,如果 Bar 不在命名空間中,則使用new \Bar()它來使其作業。
也許您必須這樣做,new \Models\Bar()或者new \App\Models\Bar()
但我不能說真的,因為您的示例中缺少相關代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532196.html
上一篇:C#“通用值引數”
