简体   繁体   中英

Load php core classes using custom autoloader

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

Well my friends.

I got the reason why I did not load the PDO class of the PHP core.

According to my research, I do one of two things:

Or put a backslash at the time of the class instances

$conn = new \PDO (...);

Or put the PDO class in the use clause.

use api\ecobd\nucleo\Conexion, PDO;

I commented that I chose the second because the first did not work.

Thanks anyway for the help given, served me well to better target my search to solve my problem :)

Check if PDO extension is loaded and PDO works without registering your auto loader. Auto loader should not effect core classes and you don't need an auto loader to load core classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM