だから私はクラスを持っています:
<?php
class Database extends PDO implements DatabaseCore
{
private $connections;
private $runQueryType = array('select'=>array(),
'update'=>array(),
'insert'=>array(),
'delete'=>array());
private $activeDB;
private $databaseKeys = array();
function __construct()
{
global $databases;
foreach ($databases as $key=>$value) {
$this->openConnection($value['dsn'], $value['user'], $value['password'], $key);
array_push($this->databaseKeys, $key);
$this->enumerateQueryRights($key, $value['rights']);
Query::initQuery();
}
}
function __destruct()
{
foreach ($this->databaseKeys as $key) {
unset($this->connections->$key);
}
}
function enumerateQueryRights($key, array $rights = array())
{
foreach ($rights as $r) {
array_push($this->runQueryType[$r], $key);
}
}
public function getConnection($key = 'mysqli')
{
//ERROR_HERE: PHP says $this is not an object at this point??
return $this->connections->$key;
}
function parseConnectionInfo()
{
}
function getRightByKey($key, $op = 'select')
{
return in_array($key, $this->runQueryType[$op]);
}
function closeConnection($key)
{
if (isset($this->connections->$key)) {
unset($this->connections->$key);
return TRUE;
} else {
return FALSE;
}
}
function getConnectionInfo($key)
{
}
function getConnectionKeys()
{
return array_values($this->databaseKeys);
}
function openConnection($dsn, $user, $password, $key = 'mysqli')
{
try {
$this->connections->$key = new PDO($dsn, $user, $password);
$this->connections->$key->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
$this->connections->$key->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception('Error: connection to Database');
error_log($key . '_CONNECT_ERROR' . "\t" . $e);
set_message('We\'ve encountered an error. To try again, please log into <a href="https://powerschool.bps101.net">Powerschool</a>.', 'error');
mail('Jason.ott@bps101.net', 'Error connecting to '.$key, $e);
die('We can\'t connect to our database, an administrator has been notified');
}
}
public function getError()
{
}
}//class
関数 getConnection($key = 'mysqli') では、 $this->connections-$key; という戻り値があります。
その時点でphpがスローするエラーは次のとおりです。
Fatal error: Using $this when not in object context in /var/www/html/projects/fees/library/class/database.inc on line 40 (if I counted right, look for the comment in the code, ERROR_HERE)
スクリプトのその時点で $this がオブジェクトと見なされない理由がよくわかりません。アドバイスをいただければ幸いです。さらに詳しい情報が必要な場合は、お問い合わせください。