I am following some examples in the PRO PHP AND JQUERY
book, but for some reason the examples doesn't work. Even the examples that I downloaded from the books site doesn't work. Not sure whats wrong, because I have done exactly 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 since every chapter in the books builds on this simple foundation. My guess is that __autoload()
is the problem, but I have no idea..
The filepaths do not point to the right spots.
A better idea would be something like this in Index.php
...
define('DOCROOT', dirname(__FILE__));
...and then modify your __autoload()
like so...
function __autoload($class)
{
$filename = DOCROOT . "/sys/class/class." . strtolower($class) . ".inc.php";
if ( file_exists($filename) )
{
include_once $filename;
}
}
You should strotlower()
the filename before including it, as your class is Calendar
but your filename has calendar
.
First of all, you are discouraged from using __autoload() in new code as it MAY BE DEPRECATED OR REMOVED IN THE FUTURE, according to official doc; instead, use spl_autoload_register() like so:
//first define a custom function
function myAutoLoader( $className ){
$path = strtolower( path/to/your/class/ . $className . '.php' );
include_once( $path );
}
Notice how we strtolower() the $className. This to make sure that the class name ( usually starts with upper case ) tallies with the filename ( usually all in lower case ). In some environment ( especially Windows ), this may not be necessary, but just to be on a safer side. For example, I do not have to in my development environment which is Windows but my production environment( debian ) will not take it.
Then pass that function name as argument into spl_autoload_regsiter. Note that the function name is string
//Now use spl_autoload_register()
spl_autoload_register( 'myAutoLoader' );
If you wish to catch exception, you may do something like this in your custom function like so:
//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' );
You will then have to catch the thrown exception when declaring your class.
I got this type error by use '
replaced "
,if use "
, there is no this error:
The right writing:
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";
}
}
The wrong writing:
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';
}
}
精彩评论