1--TEST-- 2Testing several callbacks using PDO::FETCH_FUNC 3--SKIPIF-- 4<?php 5if (!extension_loaded('pdo_sqlite')) print 'skip not loaded'; 6?> 7--FILE-- 8<?php 9 10$db = new PDO('sqlite::memory:'); 11$db->exec('CREATE TABLE testing (id INTEGER , name VARCHAR)'); 12$db->exec('INSERT INTO testing VALUES(1, "php")'); 13$db->exec('INSERT INTO testing VALUES(2, "")'); 14 15$st = $db->query('SELECT * FROM testing'); 16$st->fetchAll(PDO::FETCH_FUNC, function($x, $y) use ($st) { var_dump($st); print "data: $x, $y\n"; }); 17 18$st = $db->query('SELECT name FROM testing'); 19var_dump($st->fetchAll(PDO::FETCH_FUNC, 'strtoupper')); 20 21$st = $db->query('SELECT * FROM testing'); 22var_dump($st->fetchAll(PDO::FETCH_FUNC, 'nothing')); 23 24$st = $db->query('SELECT * FROM testing'); 25var_dump($st->fetchAll(PDO::FETCH_FUNC, '')); 26 27$st = $db->query('SELECT * FROM testing'); 28var_dump($st->fetchAll(PDO::FETCH_FUNC, NULL)); 29 30$st = $db->query('SELECT * FROM testing'); 31var_dump($st->fetchAll(PDO::FETCH_FUNC, 1)); 32 33$st = $db->query('SELECT * FROM testing'); 34var_dump($st->fetchAll(PDO::FETCH_FUNC, array('self', 'foo'))); 35 36class foo { 37 public function method($x) { 38 return "--- $x ---"; 39 } 40} 41class bar extends foo { 42 public function __construct($db) { 43 $st = $db->query('SELECT * FROM testing'); 44 var_dump($st->fetchAll(PDO::FETCH_FUNC, array($this, 'parent::method'))); 45 } 46 47 static public function test($x, $y) { 48 return $x .'---'. $y; 49 } 50 51 private function test2($x, $y) { 52 return $x; 53 } 54 55 public function test3($x, $y) { 56 return $x .'==='. $y; 57 } 58} 59 60new bar($db); 61 62$st = $db->query('SELECT * FROM testing'); 63var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test'))); 64 65$st = $db->query('SELECT * FROM testing'); 66var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test2'))); 67 68$st = $db->query('SELECT * FROM testing'); 69var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test3'))); 70 71$st = $db->query('SELECT * FROM testing'); 72var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'inexistent'))); 73 74?> 75--EXPECTF-- 76object(PDOStatement)#%d (1) { 77 ["queryString"]=> 78 %string|unicode%(21) "SELECT * FROM testing" 79} 80data: 1, php 81object(PDOStatement)#%d (1) { 82 ["queryString"]=> 83 %string|unicode%(21) "SELECT * FROM testing" 84} 85data: 2, 86array(2) { 87 [0]=> 88 %string|unicode%(3) "PHP" 89 [1]=> 90 %string|unicode%(0) "" 91} 92 93Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: function 'nothing' not found or invalid function name in %s on line %d 94bool(false) 95 96Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: function '' not found or invalid function name in %s on line %d 97bool(false) 98 99Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: no array or string given in %s on line %d 100bool(false) 101 102Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: no array or string given in %s on line %d 103bool(false) 104 105Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: class 'PDOStatement' does not have a method 'foo' in %s on line %d 106bool(false) 107array(2) { 108 [0]=> 109 %string|unicode%(9) "--- 1 ---" 110 [1]=> 111 %string|unicode%(9) "--- 2 ---" 112} 113array(2) { 114 [0]=> 115 %string|unicode%(7) "1---php" 116 [1]=> 117 %string|unicode%(4) "2---" 118} 119 120Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: cannot access private method bar::test2() in %s on line %d 121bool(false) 122array(2) { 123 [0]=> 124 %string|unicode%(7) "1===php" 125 [1]=> 126 %string|unicode%(4) "2===" 127} 128 129Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: class 'bar' does not have a method 'inexistent' in %s on line %d 130bool(false) 131