__autoload ($ class) not working? class error not found

I follow some examples in the book PRO PHP AND JQUERY, but for some reason the examples do not work. Even the examples that I downloaded from the book site do not work. Not sure what happened, because I did exactly the same as in the book.

/public/Index.php

include_once '../sys/core/init.inc.php';
$cal = new Calendar($dbo, "2010-01-01 12:00:00"); //ERROR Class 'Calendar' not found

/sys/core/init.inc.php

    function __autoload($class)
    {
        $filename = "../sys/class/class." . $class . ".inc.php";
        if ( file_exists($filename) )
        {
            include_once $filename;
        }
    }

/sys/class/class.calendar.inc.php

class Calendar extends DB_Connect
{
    private $_useDate;
    private $_m;
    private $_y;
    private $_daysInMonth;
    private $_startDay;

    /**
     * Create a database containg relevant info
     *
     * @param object $dbo a database object
     * @param string $useDate the date to build calender
     */

    public function __construct($dbo=NULL, $useDate=NULL)
    {
        /*
         * Call the parent constructor to check db object 
         */
        parent::__construct($dbo); 
    }


}

This is very annoying, as each chapter of the book is based on this simple foundation. I guess the problem is __autoload(), but I have no idea.

+5
source share
3 answers

File paths do not indicate correct points.

A better idea would be something like this in Index.php...

define('DOCROOT', dirname(__FILE__));

... __autoload() , ...

function __autoload($class)
{
    $filename = DOCROOT . "/sys/class/class." . strtolower($class) . ".inc.php";
    if ( file_exists($filename) )
    {
        include_once $filename;
    }
}

strotlower() , , Calendar, Calendar.

+3

, __autoload() , , ; spl_autoload_register() :

//first define a custom function
function myAutoLoader( $className ){

    $path = strtolower( path/to/your/class/ . $className . '.php' );

    include_once( $path );

}

, strtolower() $className. , , ( ) ( ). ( Windows) , . , , Windows, (debian) .

spl_autoload_regsiter. , -

 //Now use spl_autoload_register()
spl_autoload_register( 'myAutoLoader' );

, - , :

//first define a custom function with exception handling
function myAutoLoader( $className ){

    $path = strtolower( path/to/your/class/ . $className . '.php' );

    include_once( $path );

    if( !class_exists( $className, false ) ){
       throw new RuntimeException( 'Class '. $className . ' has not been         
       loaded yet' ); 
    }
}

//then the spl_autoload_register(), just like before
spl_autoload_register( 'myAutoLoader' );

.

+1

, ' replace ", ", :

:

function __autoload($class) {

    echo HOME_INC."/$class.class.php";

    if(is_file(HOME_INC."/$class.class.php")){

        include_once HOME_INC."/$class.class.php";
    }elseif(is_file(ADMIN_INC."/$class.class.php")){

        include_once ADMIN_INC."/$class.class.php";
    }
}

:

function __autoload($class) {

    echo HOME_INC.'/$class.class.php';

    if(is_file(HOME_INC.'/$class.class.php')){

        include_once HOME_INC.'/$class.class.php';
    }elseif(is_file(ADMIN_INC.'/$class.class.php')){

        include_once ADMIN_INC.'/$class.class.php';
    }
}
0

All Articles