扩展PDO语句类 [英] Extending PDO Statement Class
问题描述
是否可以扩展PHP PDO语句类以向其添加自定义方法?这与扩展基本PDO类不同.如果是这样,那怎么办呢,因为只有通过PDO类运行查询时才返回语句类?
Is it possible to extend PHP PDO statement class to add custom methods to it? This would be different from extending the base PDO class. If so, how would one go about doing it since the statement class is only returned when running queries through the PDO class?
推荐答案
您可以使用 PDO::setAttribute()
:
You can set the class with PDO::setAttribute()
:
PDO :: ATTR_STATEMENT_CLASS:设置用户提供的从PDOStatement派生的语句类.不能与持久性PDO实例一起使用.需要数组(字符串类名,数组(混合构造函数_args)).
PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).
示例:
$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Custom::class]);
这篇关于扩展PDO语句类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!