Home  >  Article  >  Backend Development  >  spl_autoload_register加载失败时怎么办

spl_autoload_register加载失败时怎么办

WBOY
WBOYOriginal
2016-06-13 13:44:401602browse

spl_autoload_register加载失败时怎么处理?
今天学习时碰到了两个问题,首先说下我用的是WAMP,PHP5.3.9,
在学习自动加载类的函数时
1.__autoload($className)死活不能用,我在里面echo,但没输出,那么PHP并没有自动调用它。

2.当1行不通时,我发现了spl_autoload_register,这个确实能用。不过我有个疑问,当确实没有所需的类文件时,该怎么去处理比较友好呢?例如http://localhost/index.php?action=Unknow 确实是不存在这个UnknowAction.class.php,这情况下是fatal。我想跳转到另外一个error.php页面。

谢谢

------解决方案--------------------

PHP code

<?php function __autoload($name)
{
    echo $name;
    exit;
}

$csdn = new csdn();
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
想跳转的话用header函数或输出一段js来跳转就行了。
<br><font color="#e78608">------解决方案--------------------</font><br>最好不要用这个啊,不太灵活的。 <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn