Reputation: 125
Hi guys could you help me with next.
I have next code
index.php
set_include_path(get_include_path()
.PATH_SEPARATOR.'application/controllers'
.PATH_SEPARATOR.'application/models'
.PATH_SEPARATOR.'application/views');
spl_autoload_register();
$front = new Controller;
$front->route();
Controller.php
public function route(){
if(class_exists($this->getController())){...}......
I have one way. Insted of using spl_autoload_register();
I can write
function __autoload($classname) {
@include_once $classname.'.php';
}
But according php documentation, I want to use spl_autoload_register... If you need full code, I'll be glad to demonstrate it.
Thank you!
Upvotes: 1
Views: 284
Reputation: 8301
You could just use file_exists, I suppose...
http://php.net/manual/en/function.file-exists.php
if( file_exists( 'path/to/' . $classname / '.php' ) )
{
include_once( $classname.'.php' );
}
Why are you autoloading classes that don't exist?
You could also try this:
if( !include_once( $classname.'.php' ) )
{
//throw error, or do something... or nothing
throw new Exception( "Autoload for $classname failed. Bad path." );
}
Upvotes: 1
Reputation: 913
Here is the code that I use (I hope it will give you an idea):
<?php
Class ClassAutoloader {
function __construct() {
spl_autoload_register(array($this, 'loader'));
}
private function loader($className) {
$classPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR;
if(file_exists( $classPath.strtolower($className).'.php' )) {
include $classPath.strtolower($className).'.php' ;
} else if(file_exists( $classPath.$className.DIRECTORY_SEPARATOR.strtolower($className).'.php' )) {
include $classPath.$className.DIRECTORY_SEPARATOR.strtolower($className).'.php';
}
}
}
Upvotes: 1
Reputation: 534
You could turn off errors with
<?PHP
error_reporting(E_ALL & ~E_WARNING)
?>
Upvotes: 0