PHP的自动加载:重复的数据库连接 [英] php autoload: duplication of db connection
问题描述
为什么此autoload
类复制数据库连接?
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);
结果
object(database_pdo)[2]
protected 'connection' =>
object(PDO)[3]
数据库连接类位于名为"classes"的文件夹中,
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;
}
}
如何解决此错误?
结果应该是
object(database_pdo)[1]
protected 'connection' =>
object(PDO)[2]
如果不使用autoloader
类,我只会得到高于结果的结果,因此,我认为在autoloader
类的代码中一定存在我做错了的事情.
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...
如果我使用print_r
,
print_r($connection);
结果
Object ( [connection:protected] => PDO Object ( ) )
如果我使用其他人的 autoload 类,则会得到更多的重复项",
I get even more 'duplicates' if I use this autoload class by someone else,
object(database_pdo)[11]受保护的连接" => 对象(PDO)[13]
object(database_pdo)[11] protected 'connection' => object(PDO)[13]
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);
结果
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";
}
消息
警告: 包括(C:/wamp/www/art_on_your_doorstep_2011_MVC/global/applications/CART/classes_2/class_database_pdo.php) [function.include]:无法打开流:没有这样的文件或目录 在...
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 ...
引用此行的
-include AP_SITE."classes_2/class_".$class_name.".php";
这是我期望的,
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.
推荐答案
我猜这些数字是对象ID.这些是实例化时分配给PHP对象的序号.
At a guess, I'd say those numbers are object IDs. These are sequential numbers assigned to PHP objects as they are instantiated.
在您的代码中,对象#1是您的自动加载器,#2是您的database_pdo
类,而#3是PDO
.
In your code, object #1 is your autoloader, #2 is your database_pdo
class and #3 is PDO
.
这篇关于PHP的自动加载:重复的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!