Extended class cannot access parent class variable
Posted: Sun Sep 23, 2007 1:00 pm
I am trying to create a database->table->field model and things are not going too well. Basically the field class does not inherit the $_table variable from the table class so the $_table variable contains no value. But everything else works.
Any ideas on what I am doing wrong?
Any ideas on what I am doing wrong?
Code: Select all
<?php
class MysqliAccess {
static private $_instance = NULL;
static private $_mysqli;
protected $_table;
protected $_result;
protected $_where;
private function __construct () {
self::$_mysqli = mysqli_init();
self::_dbConnect();
self::_setTables();
}
final private function _dbConnect () {
self::$_mysqli->real_connect('ip address', 'user', 'password', 'table');
}
private function _setTables () {
$this->_query('SHOW TABLES');
while ($row = $this->_result->fetch_row()) {
$table = $row[0];
$this->$table = new Table($table);
}
}
final protected function _tableExists ($table) {
// removed to shorten code
}
final protected function _getTableFields ($table) {
$this->_query('SHOW COLUMNS FROM `' . $this->_escape($table) . '`');
if ($this->_result && $this->_result->num_rows > 0) {
while ($row = $this->_result->fetch_row()) {
$fields[] = $row[0];
}
return $fields;
}
return FALSE;
}
static public function getInstance () {
// removed to shorten code
}
final protected function _escape ($variable) {
// removed to shorten code
}
private function _query ($sql) {
// removed to shorten code
}
public function fetchSingle ($field) {
$this->_query('SELECT `' . $this->_escape($field) . "` FROM `$this->_table` $this->_where LIMIT 1");
if ($this->_result) return implode('', $this->_result->fetch_row());
return FALSE;
}
}
class Table extends MysqliAccess {
protected $_table;
protected function __construct ($table) {
if ($this->_tableExists($table)) $this->_table = $table;
if ($fields = $this->_getTableFields($this->_table)) {
foreach ($fields as $field) {
$this->$field = new Field($field);
}
}
}
}
class Field extends Table {
private $_field;
protected function __construct ($field) {
$this->_field = $field;
}
public function __toString () {
return $this->fetchSingle($this->_field);
}
}
$db = MysqliAccess::getInstance();
?>