开发者

__autoload is not executing in PHP

开发者 https://www.devze.com 2023-03-27 03:22 出处:网络
So I am trying to autoload classes in PHP; however, the __autoload() function does not seem to be executing. Even I try echoing the $class_name variable, I don\'t see anything except the output I have

So I am trying to autoload classes in PHP; however, the __autoload() function does not seem to be executing. Even I try echoing the $class_name variable, I don't see anything except the output I have provided below. I have included all of the relevant files and stripped out the irrelevant parts of it. According to a note in PHP: Autoloading Cl开发者_运维知识库asses - Manual, I cannot use __autoload() in CLI interactive mode, which I am not using. Any pointer will be much appreciated. Thanks.

Output of index.php:

Fatal error: Class 'Calendar' not found in /home1/tylercro/public_html/cb-test/index.php on line 3

index.php:

<?php
    require_once($_SERVER['INCLUDES'] . 'prep.php');
    $smarty -> assign('calendar', new Calendar());
?>

prep.php:

<?php
    error_reporting(-1);

    function __autoload($class_name) {
        include($_SERVER['CLASSES'] . $class_name . '.php');
    }
    require_once($_SERVER['SMARTY_BIN'] . 'Smarty.class.php');

    $smarty = new Smarty();
?>

.htaccess:

Options -Indexes

SetEnv CLASSES /home1/tylercro/public_html/cb-test/includes/classes/
SetEnv INCLUDES /home1/tylercro/public_html/cb-test/includes/
SetEnv SMARTY_BIN /home1/tylercro/smarty/


Smarty has its own autoload function, and it collides with yours. spl_autoload_register() can solve your problem, as it can register any function as an autoloader.


This post solved my issue. All I had to do was change prep.php to the following:

<?php
    error_reporting(-1);

    function __autoload($class_name) {
        include($_SERVER['CLASSES'] . $class_name . '.php');
    }
    define('SMARTY_SPL_AUTOLOAD',1);
    require_once($_SERVER['SMARTY_BIN'] . 'Smarty.class.php');
    spl_autoload_register('__autoload');

    $smarty = new Smarty();
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消