First off, i like to tell you my directory structure
- /var/www
|- /social_network/
|- index.php
|- /application/
|-/controllers/
|-user.php
|- /models/
|- framework.php
|-/tmp/
In my index.php i include application/framework.php here is my framework.php code
<?php
spl_autoload_register();
class Framework{
public function load($page, $data){
if(is_array($data)){
extract($data);
}
include "views/".$page;
}
}
$object = "controller\\$controller";
$object = new $object;
if(method_exists($object,"$method")){
$object->$method();
} else {
show_404();
}
?>
Now with the above code, auto loading my class works fine. In case you are wondering $method and $controller comes from index.php depending on the URI.
Now a friend of mine told me this is wrong way to do it so i changed my code to
<?php
function autoload_controller($controller){
include "$controller.php";
}
spl_autoload_register('autoload_controller');
class Framework{
public function load($page, $data){
if(is_array($data)){
extract($data);
}
include "views/".$page;
}
}
$object = "controller\\$controller";
$object = new $object;
if(method_exists($object,"$method")){
$object->$method();
} else {
show_404();
}
?>
Now i get this error below
Warning: include(controller\User.php): failed to open stream: No such file or directory in /var/www/social_network/application/framework.php on line 3 Warning: include(): Failed opening 'controller\User.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/social_network/application/framework.php on line 3 Fatal error: Class 'controller\User' not found in /var/www/social_network/application/framework.php on line 16
How can i fix this, i have been at this for hours and also if it is possible please show me how i can call a model from models directory from inside a controller class using autoload Thanks
set_include_path(get_include_path().PATH_SEPARATOR.'/var/www/social_network/application/');
or some constant instead of hard coded string