Let's see if I can describe this properly...
I have an abstract class that when other classes extend from it, I'd like for the abstract class' data to be reset to zero.
The idea being that I have a pile of classes extending this and using MySQL table's for data structure. I don't want to query the DB with every class instantiation to determine the class' data ("SHOW COLUMNS FROM tablename
).
So for every class, I'd like for it to each "Have we created this class before? If so, we know the class' structure, if not, grab the table columns and create the class as well as store the table columns for later use."
I've been using the following for testing my idea:
$columns = array("Column 1", "Column 2", "Column 3");
abstract class AbstractClass {
protected static $colFields = array();
public function __construct() {
$this->setVars();
}
private function setVars() {
global $columns;
if (count(self::$colFields) == 0) {
var_dump("Array length is 0");
foreach ($columns as $key) {
self::$colFields[] = $key;
if (!isset($this->$key))
$this->$key = null;
}
}
else {
var_dump("Array length is not 0");
foreach (self::$colFields as $key) {
$this->$key = null;
}
}
}
public function test() {
var_dump($this);
}
}
class ObjectA extends AbstractClass {};
class ObjectB extends AbstractClass {};
$objectAA = new ObjectA();
$objectAB = new ObjectA();
$objectAC = new ObjectA();
$objectAC->test();
$objectBA = new ObjectB();
$objectBB = new ObjectB();
$objectBC = new ObjectB();
$objectBC->test();
And the script's output is:
string(17) "Array length is 0"
string(21) "Array length is not 0"
string(21) "Array length is not 0"
object(ObjectA)#3 (4) {
["className":protected]=>
string(7) "ObjectA"
["Column 1"]=>
NULL
["Column 2"]=>
NULL
["Column 3"]=>
NULL
}
string(21) "Array length is not 0"
string(21) "Array length is not 0"
string(21) "Array length is not 0"
object(ObjectB)#6 (4) {
["className":protected]=>
string(7) "ObjectB"
["Column 1"]=>
NULL
["Column 2"]=>
NULL
["Column 3"]=>
NULL
}
I'm expecting the first instantiation of ObjectB to output the "Array length is 0" segment, then continue on.
Can anyone help?