1--TEST-- 2Testing several callbacks using PDO::FETCH_FUNC 3--EXTENSIONS-- 4pdo_sqlite 5--FILE-- 6<?php 7 8$db = new PDO('sqlite::memory:'); 9$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 10 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 21try { 22 $st = $db->query('SELECT * FROM testing'); 23 var_dump($st->fetchAll(PDO::FETCH_FUNC, 'nothing')); 24} catch (\TypeError $e) { 25 echo $e->getMessage(), \PHP_EOL; 26} 27 28try { 29 $st = $db->query('SELECT * FROM testing'); 30 var_dump($st->fetchAll(PDO::FETCH_FUNC, '')); 31} catch (\TypeError $e) { 32 echo $e->getMessage(), \PHP_EOL; 33} 34 35try { 36 $st = $db->query('SELECT * FROM testing'); 37 var_dump($st->fetchAll(PDO::FETCH_FUNC, NULL)); 38} catch (\TypeError $e) { 39 echo $e->getMessage(), \PHP_EOL; 40} 41 42try { 43 $st = $db->query('SELECT * FROM testing'); 44 var_dump($st->fetchAll(PDO::FETCH_FUNC, 1)); 45} catch (\TypeError $e) { 46 echo $e->getMessage(), \PHP_EOL; 47} 48 49try { 50 $st = $db->query('SELECT * FROM testing'); 51 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('self', 'foo'))); 52} catch (\TypeError $e) { 53 echo $e->getMessage(), \PHP_EOL; 54} 55 56class foo { 57 public function method($x) { 58 return "--- $x ---"; 59 } 60} 61class bar extends foo { 62 public function __construct($db) { 63 $st = $db->query('SELECT * FROM testing'); 64 var_dump($st->fetchAll(PDO::FETCH_FUNC, array($this, 'parent::method'))); 65 } 66 67 static public function test($x, $y) { 68 return $x .'---'. $y; 69 } 70 71 private function test2($x, $y) { 72 return $x; 73 } 74 75 public function test3($x, $y) { 76 return $x .'==='. $y; 77 } 78} 79 80new bar($db); 81 82$st = $db->query('SELECT * FROM testing'); 83var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test'))); 84 85try { 86 $st = $db->query('SELECT * FROM testing'); 87 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test2'))); 88} catch (\TypeError $e) { 89 echo $e->getMessage(), \PHP_EOL; 90} 91 92try { 93 $st = $db->query('SELECT * FROM testing'); 94 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test3'))); 95} catch (\TypeError $e) { 96 echo $e->getMessage(), \PHP_EOL; 97} 98 99try { 100 $st = $db->query('SELECT * FROM testing'); 101 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'inexistent'))); 102} catch (\TypeError $e) { 103 echo $e->getMessage(), \PHP_EOL; 104} 105 106?> 107--EXPECTF-- 108object(PDOStatement)#%d (1) { 109 ["queryString"]=> 110 string(21) "SELECT * FROM testing" 111} 112data: 1, php 113object(PDOStatement)#%d (1) { 114 ["queryString"]=> 115 string(21) "SELECT * FROM testing" 116} 117data: 2, 118array(2) { 119 [0]=> 120 string(3) "PHP" 121 [1]=> 122 string(0) "" 123} 124function "nothing" not found or invalid function name 125function "" not found or invalid function name 126PDOStatement::fetchAll(): Argument #2 must be a callable, null given 127no array or string given 128cannot access "self" when no class scope is active 129array(2) { 130 [0]=> 131 string(9) "--- 1 ---" 132 [1]=> 133 string(9) "--- 2 ---" 134} 135array(2) { 136 [0]=> 137 string(7) "1---php" 138 [1]=> 139 string(4) "2---" 140} 141non-static method bar::test2() cannot be called statically 142non-static method bar::test3() cannot be called statically 143class bar does not have a method "inexistent" 144