簡體   English   中英

php類退出並自動加載

[英]php class exits and autoload

嘗試檢查類是否使用php的class_exits函數退出。 但是,我有__autoload並注冊了一個在類不可用時回顯或退出的函數。 問題是,在檢查php類退出功能期間,回顯if(class_exist($ class))返回false,這是不受歡迎的行為。 我該怎么做才能停止這種行為。

$file=core_directory.'/plugins/'.$class.'.php';
if(is_readable($file))
require($file);
else
echo " The file:".$file." does not exist on this server";

工作正常,但如果這

if(class_exists($controller))
{
    return new $controller_name($this);
}
else
{
    return $this->controller_dosnt_exist($controller_name);
}

返回false會回顯我的__autoload錯誤,這不是我想要的,尤其是因為我想將其替換為exit($ msg)

我試過了

if(new $controller())
{
    return new $controller_name($this);
}
else
{
    return $this->controller_dosnt_exist($controller_name);
}

而且效果很好,所以我的問題是我的第二個問題,如果由於某種內存原因而不能使用該語句,或者如果這樣,我該如何為我的第一部分代碼獲得所需的行為。

不要在自動裝帶器中回顯。 如果您需要跟蹤這些問題,請將它們記錄到某個位置的文件中。 您可以將file_put_contents與FILE_APPEND標志一起使用,以實現簡單的實現。

您也可以將class_exists包裝在ob_start / ob_clean中,但這非常丑陋。

我第一次錯聽了你的問題。 這是您可能正在尋找的東西:

1,將您的“ echo”替換為如下異常:

$file=core_directory.'/plugins/'.$class.'.php';

if(is_readable($file))
    require($file);
else
    throw new Exception("The file:".$file." does not exist on this server");

現在,將您的if語句替換為:

$ret = null;

try{
    $ret = new $controller_name($this); //Idk what you're doint with '$this', so i kept it
}catch(Exception $ex){
    $ret = $this->controller_dosnt_exist($controller_name);
}

return $ret;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM