PHP5 &抽象类.每个子类的类变量的单独副本? [英] PHP5 & Abstract Classes. Separate copy of class variables for each child class?

查看:22
本文介绍了PHP5 &抽象类.每个子类的类变量的单独副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看看我是否能恰当地描述这一点...

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.

这个想法是我有一堆类扩展它并使用 MySQL 表的数据结构.我不想在每个类实例化时查询数据库以确定类的数据(SHOW COLUMNS FROM tablename).

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();

脚本的输出是:

string(17) "数组长度为 0"
string(21) "数组长度不为 0"
string(21) "数组长度不为 0"
object(ObjectA)#3 (4) {
["className":protected]=>
string(7) "ObjectA"
["第 1 列"]=>

["第 2 列"]=>

["第 3 列"]=>

}
string(21) "数组长度不为 0"
string(21) "数组长度不为 0"
string(21) "数组长度不为 0"
object(ObjectB)#6 (4) {
["className":protected]=>
string(7) "ObjectB"
["第 1 列"]=>

["第 2 列"]=>

["第 3 列"]=>

}

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
}

我期待 ObjectB 的第一个实例输出数组长度为 0"段,然后继续.

I'm expecting the first instantiation of ObjectB to output the "Array length is 0" segment, then continue on.

有人可以帮忙吗?

推荐答案

我建议您不要将缓存的表信息存储在您的抽象类中.创建一个名为 TableInformationRepository 或类似的新类.让您的子类或抽象类查询 TableInformationRepository 以获取有关表/类的信息.在 TableInformationRepository 中,缓存您要按类名或表名键入的信息.

I suggest that you do not store the cached table information in your abstract class. Create a new class called TableInformationRepository or something like that. Have your child class or abstract class query the TableInformationRepository for information about tables/classes. In the TableInformationRepository, cache the information you want keyed by class name or table name.

这篇关于PHP5 &抽象类.每个子类的类变量的单独副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆