I've been searching the database of questions on the forum, but can not get a solution that is oriented to my question.
I come to you, because I doubt has arisen with an autoloader that I'm developing.
The point is this, when I try to instantiate a class defined by me the autoloader works fine, but when I try to instantiate a class of the PHP core throws me the following error.
For example, when I try to do this:
$conn = new PDO (...);
Then throws me the following error:
Fatal error: Class 'PDO' not found in ...
I've noticed that the autoloader is trying to load the class from which I have defined routes to my classes.
My question is, how I can do to load a class of the PHP core if I'm using a custom autoloader?
I hope you can guide me to solve this problem that I have been presented.
In advance thank you very much.
Excuse me for not placing the code of custom autoloader, I missed.
The code is the same used in symfony but modified and simplified.
class ClassLoader {
private $prefixes = array ();
private $fallbackDirs = array ();
private $useIncludePath = false;
public function getPrefixes () {
return $this->prefixes;
}
public function getFallbackDirs () {
return $this->fallbackDirs;
}
public function addPrefixes ( array $prefixes ) {
foreach ( $prefixes as $prefix => $path ) {
$this->addPrefix ( $prefix, $path );
}
}
public function addPrefix ( $prefix, $paths ) {
if ( !$prefix ) {
foreach ( ( array ) $paths as $path ) {
$this->fallbackDirs [] = $path;
}
return;
}
if ( isset ( $this->prefixes [ $prefix ] ) ) {
$this->prefixes [ $prefix ] = array_merge ( $this->prefixes [ $prefix ], ( array ) $paths );
} else {
$this->prefixes [ $prefix ] = ( array ) $paths;
}
}
public function setUseIncludePath ( $useIncludePath ) {
$this->useIncludePath = $useIncludePath;
}
public function getUseIncludePath () {
return $this->useIncludePath;
}
public function register ( $prepend = false ) {
spl_autoload_register ( array ( $this, 'loadClass' ) , true, $prepend );
}
public function unregister () {
spl_autoload_unregister ( array ( $this, 'loadClass' ) );
}
public function loadClass ( $class ) {
if ( $file = $this->findFile ( $class ) ) {
require $file;
return true;
}
}
public function findFile ( $class ) {
if ( '\\' == $class [ 0 ] ) {
$class = substr ( $class, 1 );
}
if ( false !== $pos = strrpos ( $class, '\\' ) ) {
// namespaced class name
$classPath = str_replace ( '\\', DIRECTORY_SEPARATOR, substr ( $class, 0, $pos ) ) . DIRECTORY_SEPARATOR;
$className = substr ( $class, $pos + 1 );
} else {
// PEAR-like class name
$classPath = null;
$className = $class;
}
$classPath .= str_replace ( '_', DIRECTORY_SEPARATOR, $className ) . '.php';
foreach ( $this->prefixes as $prefix => $dirs ) {
if ( 0 === strpos ( $class, $prefix ) ) {
foreach ( $dirs as $dir ) {
if ( file_exists ( $dir . DIRECTORY_SEPARATOR . $classPath ) ) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}
}
}
foreach ( $this->fallbackDirs as $dir ) {
if ( file_exists ( $dir . DIRECTORY_SEPARATOR . $classPath ) ) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}
if ( $this->useIncludePath && $file = stream_resolve_include_path ( $classPath ) ) {
return $file;
}
}
}
But if you want to see the original file is here
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/ClassLoader/ClassLoader.php