PHP5&抽象类。每个子类的类变量的单独副本?
让我们看看我是否可以正确地描述这一点...
我有一个抽象类,当其他类从它扩展时,我希望将抽象类的数据重置为零。
我的想法是,我有一堆类来扩展它并使用 MySQL 表作为数据结构。我不想用每个类实例化来查询数据库以确定类的数据(“SHOW COLUMNS FROM tablename
”)。
因此,对于每个类,我希望每个“我们是否之前创建过这个类吗?如果是这样,我们就知道类的结构,如果不是,则获取表列并创建类并存储表列以供以后使用。”
我一直在使用以下内容来测试我的想法:
$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)“对象A”
[“第 1 列”]=>
空
[“第2栏”]=>
空
[“第3栏”]=>
空
}
string(21) "数组长度不为 0"
string(21) "数组长度不为 0"
string(21) "数组长度不为 0"
对象(ObjectB)#6 (4) {
["className":protected]=>
字符串(7)“对象B”
[“第 1 列”]=>
空
[“第2栏”]=>
空
[“第3栏”]=>
空
}
的第一个实例化输出“数组长度为 0”段,然后继续。
有人可以帮忙吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议你不要将缓存的表信息存储在你的抽象类中。创建一个名为 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.
补充一下 Scott Saunders 所说的——为什么不使用单例呢?
http://php.net/manual/en/language.oop5.patterns。 php - 示例#2
To add to what Scott Saunders said - why not use a singleton for that?
http://php.net/manual/en/language.oop5.patterns.php - example #2
在 PHP 中,如果不在子类中重新声明受保护/公共静态字段,它们都将属于同一引用集。这意味着受保护属性的更改将反映在所有类中。
示例:
另一方面:
您也可以手动中断参考集:
In PHP, if you don't redeclare protected/public static fields in the subclasses, they will all belong to the same reference set. This means a change in a protected property will be reflected in all the classes.
Example:
On the other hand:
You can also break the reference set by hand:
感谢您的详细解释,我在其他网站上找到了非常基本的信息希望这对其他人有帮助。
http://www.phptechi.com/php-5-abstract-class.html
thanks for detailed explanation, I found very basic information on other site hope this help others.
http://www.phptechi.com/php-5-abstract-class.html