問題描述
是否可以擴展 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]);
這篇關于擴展 PDO 語句類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!