php autoload: duplication of db connection

痞子三分冷 提交于 2019-12-08 10:47:08

问题


Why does this autoload class duplicate the db connection?

class autoloader
{
    private $directory_name;

    public function __construct($directory_name)
    {
        $this->directory_name = $directory_name;
    }

    public function autoload($class_name) 
    { 
        $file_name = 'class_'.strtolower($class_name).'.php';

        $file = AP_SITE.$this->directory_name.'/'.$file_name;

        if (file_exists($file) == false)
        {
            return false;
        }
        include ($file);
    }
}

# nullify any existing autoloads
spl_autoload_register(null, false);

# specify extensions that may be loaded
spl_autoload_extensions('.php, .class.php, .lib.php');

# instantiate the autoloader object
$classes_2 = new autoloader('classes');

# register the loader functions
spl_autoload_register(array($classes_2, 'autoload'));

$connection = new database_pdo(DSN,DB_USER,DB_PASS);
var_dump($connection);

result,

object(database_pdo)[2]
  protected 'connection' => 
    object(PDO)[3]

The db connection class is located in a folder called 'classes',

class database_pdo
{
    # database handler
    protected $connection = null;

    # make a connection
    public function __construct($dsn,$username,$password)
    {
        try 
        {
            # MySQL with PDO_MYSQL  
            $this->connection = new PDO($dsn, $username, $password);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        }
        catch (PDOException $e) 
        {
            # call the get_error function
            $this->get_error($e);
        }
    }

    ...

    # closes the database connection when object is destroyed
    public function __destruct()
    {
        # set the handler to NULL closes the connection propperly
        $this->connection = null;
    }
}

How can I fix this mistake?

EDIT:

the result it should be,

object(database_pdo)[1]
      protected 'connection' => 
        object(PDO)[2]

I only get above as the result if I don't use the autoloader class, so I think there must be something I have done wrong in the code of autoloader class.

But I don't know which line is wrong...

EDIT:

If I use print_r,

print_r($connection);

Result,

Object ( [connection:protected] => PDO Object ( ) )

EDIT:

I get even more 'duplicates' if I use this autoload class by someone else,

object(database_pdo)[11] protected 'connection' => object(PDO)[13]

EDIT:

function __autoload($class_name) 
{
    include AP_SITE."classes_1/class_".$class_name.".php";
}

$connection = new database_pdo(DSN,DB_USER,DB_PASS);
var_dump($connection);

result,

object(database_pdo)[1]
  protected 'connection' => 
    object(PDO)[2]

but I will get error if I want to load classes from other directories,

function __autoload($class_name) 
{
    include AP_SITE."classes_1/class_".$class_name.".php";
    include AP_SITE."classes_2/class_".$class_name.".php";
}

message,

Warning: include(C:/wamp/www/art_on_your_doorstep_2011_MVC/global/applications/CART/classes_2/class_database_pdo.php) [function.include]: failed to open stream: No such file or directory in ...

which refers to this line - include AP_SITE."classes_2/class_".$class_name.".php";

EDIT:

This one is working like I expect,

function system_autoload($class) {
   // $class = str_replace('_', '/', strtolower($class.'.php'));
    if(file_exists(AP_SITE.'classes_1/class_'.$class.'.php')) {
        include_once(AP_SITE.'classes_1/class_'.$class.'.php');

    } else if(file_exists(AP_SITE.'classes_2/class_'.$class.'.php')) {
        include_once(AP_SITE.'classes_2/class_'.$class.'.php');
    }
}
spl_autoload_register('system_autoload');

But it is not generic at all as I have duplicate the code inside when I have more than 2 directories.


回答1:


At a guess, I'd say those numbers are object IDs. These are sequential numbers assigned to PHP objects as they are instantiated.

In your code, object #1 is your autoloader, #2 is your database_pdo class and #3 is PDO.



来源:https://stackoverflow.com/questions/7711450/php-autoload-duplication-of-db-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!