如何在PDO :: FETCH_FUNC中使用对象方法

Ita*_*vka 3 php pdo callback

这个问题参考 了PHP手册中的以下 http://www.php.net/manual/en/pdostatement.fetchall.php.

这使我能够在获取查询之前传递一个函数来处理查询的结果.

我想将一个方法作为函数传递给对象.

$this如何引用该对象我将如何编写它?

tle*_*nss 5

如果您在课堂范围之外工作.你可以这样做

// SELECT id, title FROM pages

$result = $sth->fetchAll(PDO::FETCH_FUNC, array('Foo', 'bar'));

Class Foo {
public function bar($id, $name) { return $id . " : " . $name;}
}
Run Code Online (Sandbox Code Playgroud)

真正使用$ this也是如此

$result = $sth->fetchAll(PDO::FETCH_FUNC, array($this, 'bar'));
Run Code Online (Sandbox Code Playgroud)