我使用Propel和CodeIgniter一起使用。我做了一個MY_Model
類(它擴展了CI_Model
),它使用它的構造函數加載Propel。在擴展類中使用名稱空間
如果你很好奇:
class MY_Model extends CI_Model{
public function __construct(){
parent::__construct();
require_once '/propel/runtime/lib/Propel.php';
Propel::init('/build/conf/project-conf.php');
set_include_path('/build/classes'.PATH_SEPARATOR.get_include_path());
}
}
所以,現在當我提出一個新的笨模型,它將裝載的Propel我。事情是,我給一些Propel生成的模型添加了命名空間。我想我可以在模型的構造函數中添加use Reports;
行,但不可以。
class Reports_model extends MY_Model{
function __construct(){
parent::__construct();
use Reports;
}
}
這給了我
syntax error, unexpected T_USE
好吧,我想,讓我們嘗試把它的構造外:
class Reports_model extends MY_Model{
use Reports;
function __construct(){
parent::__construct();
}
}
現在,我得到一個較長的錯誤:
syntax error, unexpected T_USE, expecting T_FUNCTION
作爲最後的手段,我添加了use Reports;
之前的類聲明:
use Reports;
class Reports_model extends MY_Model{
function __construct(){
parent::__construct();
}
}
現在我得到更多的錯誤!
The use statement with non-compound name 'Reports' has no effect
Class 'ReportsQuery' not found
在班級的另一個功能中,我有一個行$report = ReportsQuery::create();
。
那麼,我怎樣才能得到use Reports;
線路的工作?我真的不想加入Reports\
無處不在。
我怎樣才能使它所以我可以做:的
$report = ReportsQuery::create();
代替:
$report = Reports\ReportsQuery::create();
好問題...我的後續將是爲什麼PHP不能找到'Reports'命名空間?我的猜測是'Reports_model'類的位置使得PHP不知道在哪裏找到'Reports'。對不起,我不是名字空間專家,但是如果您在這裏沒有得到答案,您可能想在Propel google小組中詢問這個問題! – jakerella
@jakerella:'Reports'命名空間只存在於Propel的模型中。我的猜測是'MY_Model'的構造函數尚未被調用,所以PHP不知道名稱空間在哪裏。 –
Riiiight ... hrm。也許你只需要始終在模型構造函數中初始化Propel?換句話說,把它放在某種啓動區域(不太熟悉CI)。 – jakerella