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

查看:113
本文介绍了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'一直使用以下方法测试我的想法:

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

对象(ObjectA)#3(4){

[ className:protected] =>

字符串(7) ObjectA

[栏1] =>

NULL

[栏2] =>

NULL

[ Column 3] =>

NULL

}

string(21)数组长度不为0

string(21)数组长度不为0

string(21)数组长度不为0

object(ObjectB)#6(4){

[ className:受保护] =>

字符串(7) ObjectB

[ Column 1] =>

NULL

[ Column 2] =>

NULL

[ Column 3] =>

NULL

}

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天全站免登陆