如何在抽象类中使用PDO :: FETCH_CLASS? [英] How to use PDO::FETCH_CLASS with abstract class?
问题描述
是否有一种方法可以基于检索到的数据使用PDO::FETCH_CLASS
实例化子类?假设存在一个抽象类A
,类B
和C
,它们是A
的实现,并且在我的表中名为classname
的字段将用于实例化正确的类.有可能吗?
Is there a method to instantiate a child class with PDO::FETCH_CLASS
based on retrieved data? Suppose that there exists an abstract class A
, classes B
and C
that are implementations of A
and a field on my table named classname
that will be used to instantiate the right class. Is it possible?
推荐答案
您需要的声音,PDO::FETCH_CLASSTYPE
. 摘自手册:
PDO :: FETCH_CLASS:返回所请求类的新实例, 将结果集的列映射到 班级.如果fetch_style包含PDO :: FETCH_CLASSTYPE(例如 PDO :: FETCH_CLASS | PDO :: FETCH_CLASSTYPE),则该类的名称为 由第一列的值确定.
PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class. If fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column.
这篇关于如何在抽象类中使用PDO :: FETCH_CLASS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!