Reputation: 193
I'm new to php & developing php5.4 application.there i used auto loading classes.I get a fatal error so I assume it doesn't work function _autoload properly.. Please find below the code I used. When I tried it gave me the following error: please help me.
Fatal error: Class 'User' not found in C:\Program Files\xampp\htdocs\MVCApp\index.php on line 35
C:\Program Files\xampp\htdocs\MVCApp\index.php file
<?php
//define('APPLICATION_PATH', realpath('../'));//C:\Program Files\xampp\htdocs
define('APPLICATION_PATH', realpath('../'));//C:\Program Files\xampp\htdocs\MVCApp
echo APPLICATION_PATH;
$paths=array(
APPLICATION_PATH,
APPLICATION_PATH.'/com',
get_include_path(),
);
set_include_path(implode(PATH_SEPARATOR, $paths));
// echo get_include_path();//C:\Program Files\xampp\php\PEAR
function _autoload($className)
{
require_once $className.'.php';
return;
}
$user=new User();
echo $user->getName();
?>
C:\Program Files\xampp\htdocs\MVCApp\com\User.php file
<?php
C:\Program Files\xampp\htdocs\MVCApp\com\User.php file
<?php
class User {
public function getName()
{
return 'Hello Sam';
}
}
Upvotes: 1
Views: 75
Reputation: 1150
function __autoload($className)
{
echo $className;
}
autoload function works with two underscores.not one.Now i think it should be work.
Upvotes: 3