gpt4 book ai didi

php - 如何从特定的 PDO 驱动程序对象中获取所有方法的完整列表?

转载 作者:可可西里 更新时间:2023-10-31 22:52:04 26 4
gpt4 key购买 nike

我试图获取每个 PDO 驱动程序的所有方法(我已将它们全部安装在我的 Windows PC 上)。

但如果我尝试,例如,使用 Postgres (pdo_pgsql):

var_export(get_class_methods('PDO'));

结果是:

array (
0 => '__construct',
1 => 'prepare',
2 => 'beginTransaction',
3 => 'commit',
4 => 'rollBack',
5 => 'inTransaction',
6 => 'setAttribute',
7 => 'exec',
8 => 'query',
9 => 'lastInsertId',
10 => 'errorCode',
11 => 'errorInfo',
12 => 'getAttribute',
13 => 'quote',
14 => '__wakeup',
15 => '__sleep',
16 => 'getAvailableDrivers',
)

但是这个列表是不完整的,因为 PDO Postgres 包括如下方法:

$pdo->pgsqlCopyToArray('my_table');

有没有办法获取所有方法而不仅仅是 PDO 默认值?

提前致谢,塞尔索

编辑:

我也试过反射,结果像get_class_methods()一样不完整:

var_export(array_column((new ReflectionClass('PDO'))->getMethods(), 'name'));

结果是一样的:

array (
0 => '__construct',
1 => 'prepare',
2 => 'beginTransaction',
3 => 'commit',
4 => 'rollBack',
5 => 'inTransaction',
6 => 'setAttribute',
7 => 'exec',
8 => 'query',
9 => 'lastInsertId',
10 => 'errorCode',
11 => 'errorInfo',
12 => 'getAttribute',
13 => 'quote',
14 => '__wakeup',
15 => '__sleep',
16 => 'getAvailableDrivers',
)

最佳答案

首先,根据 PHP Docs,您必须了解什么是 PDO:

Introduction ¶

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.

明确指出PDO是访问数据库的轻量级接口(interface)。再看看那里提供的注释

Note: that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

为了更清楚地理解 POD 中有四件事,即

PDO 接口(interface) PDO 语句 PDO 异常 PDO 驱动程序

(所有数据库相同) (所有数据库相同) (所有数据库相同) (所有数据库不同)

get_class_methods 仅列出 PDO 的方法,不列出 Statement FunctionsExceptions functions 和 < strong>驱动函数

现在认为 PDO 有四个不同的类:

类 PDO 类 PDO_Statement 类 PDO_Exections 类 PDO_Driver

语句、异常和驱动程序在内部链接到 PDO 类

现在你在 Class PDO 上调用 get_class_methods 它将显示 Class PDO 的方法,如果你想知道所有的方法PDO_Driver 那么你必须调用 PDO_Driver 类的正确名称。

现在 PGSQL 驱动程序的正确名称是什么,它是 PHP 的内部结构,为此您必须研究 PHP 内部结构。

关于php - 如何从特定的 PDO 驱动程序对象中获取所有方法的完整列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com