php类退出并自动加载

php类退出并自动加载,php,class,if-statement,autoload,Php,Class,If Statement,Autoload,尝试使用php的class_exits函数检查类是否存在。但是,我已自动加载并注册到一个函数,如果该类不可用,该函数将回显或退出。问题是,在检查php类出口的过程中,函数会回显ifclass_exist$class返回false,这是一种不希望出现的行为。我能做些什么来阻止这种行为 $file=core_directory.'/plugins/'.$class.'.php'; if(is_readable($file)) require($file); else echo " The file:

尝试使用php的class_exits函数检查类是否存在。但是,我已自动加载并注册到一个函数,如果该类不可用,该函数将回显或退出。问题是,在检查php类出口的过程中,函数会回显ifclass_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,它会回显我的uuu自动加载错误,这不是我想要的,尤其是当我想用exit$msg替换它时

我试过了

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

这很好,所以我的问题是我的第二个问题,如果出于某种内存原因或其他原因使用station不好,那么我如何才能获得第一段代码所需的行为。

不要在自动加载器中回音。如果您需要跟踪这些问题,请将它们记录到某个文件中。您可以将file_put_内容与file_APPEND标志一起使用,以实现一个简单的实现


你也可以用ob\u start/ob\u clean来包装这个类,但这太难看了。

我第一次误解了你的问题。以下是您可能要查找的内容:

首先,将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;

是否打算使用$controller和$controller\u name变量?如果是这样,它们的确切值是多少。嘿,如果$class来自用户输入,我建议在调用这些函数之前清理数据,或者将它们包含在输出中。空字节可以防止.php扩展名包含在完全限定字符串中;不,它们不是用户输入,并且它们持有像index\u controller这样的字符串。您可以执行以下操作…iffile\u exists$file{require$file;}return false;。。。好吧,我明白了,我明白了,每个人都会回应或退出“msg”。实际上,可能必须记录日志,因为回显对于它的使用来说是一个禁忌。在自动加载程序中处理丢失的类的最佳方法通常是返回并让PHP抛出一个关于找不到该类的错误,因为通常没有该类,代码就无法继续。在本例中,您正在测试类为_exists的类,因此,类为_exists的代码可能应该记录它缺少的类。好的,谢谢,对我来说似乎有点问题,但我不能做if。所以我只是想总结一下。更好的解决方案是按照这个答案所说的去做,或者干脆不回显,而是记录错误