扩展 PDO 语句类
是否可以扩展 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 实例一起使用.需要数组(字符串类名,数组(混合构造函数_参数)).
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]);
相关文章