使用PDO :: FETCH_CLASSTYPE传递构造函数参数 [英] pass constructor arguments using PDO::FETCH_CLASSTYPE
问题描述
我正在用新的基于PDO的版本替换旧数据库层.
I am replacing my old database layer by a new PDO based version.
但是我遇到了一个问题:
However i have run into a problem:
使用fetchObject
获取对象时,我可以为对象构造函数传递参数.
When fetching objects using fetchObject
i can pass arguments for the object constructor.
但是,我现在正在移植具有多个子类的类,这些子类都存储在同一张表中,并且我想使用FETCH_CLASSTYPE
.这意味着我必须使用无法将构造函数参数传递给的常规fetch()
方法.
However i am now porting over a class which has several subclasses, all stored in the same table, and i want to use FETCH_CLASSTYPE
. This means that i have to use the regular fetch()
method to which i cant pass constructor arguments.
还有另一种方法吗?
我可以将构造函数重命名为其他名称,然后手动调用它,但是我想要一个干净的解决方案.
I could rename the constructor to something else and call it manually but i would like a clean solution.
推荐答案
It's a PHP bug reported as #62567
您可以通过将带有构造函数的任何类名作为第二个参数传递来解决此问题
You may work around it by passing as a second argument any classname with a constructor
这篇关于使用PDO :: FETCH_CLASSTYPE传递构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!