我有一个问题作出PDO连接访问在扩展类,例如:不能访问子类PDO连接
class Model {
public $connection;
public function __construct(PDO $connection = null)
{
global $config;
$this->connection = $connection;
if ($this->connection === null) {
$this->connection = new PDO(
'mysql:host='.$config['db_host'].';dbname='.$config['db_name'], $config['db_username'], $config['db_password']);
$this->connection->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
}
}
而另一扩展类模型
class User extends Model {
public function someFunction(){
// how can I access the pdo connection in the parent constructer here?
}
}
这就是症结我不想访问在子类中的构造函数中创建的父连接的问题,非常感谢。
这是错误的创建模型对象的连接,反正访问父母的财产,你可以简单地使用'$ this-> connection ...'在子节点 –
由于'User extends Model',你可以访问'$ this-> connection'。 –