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 test_fetch_func_001 (id INTEGER , name VARCHAR)'); 12$db->exec('INSERT INTO test_fetch_func_001 VALUES(1, "php"), (2, "")'); 13 14$st = $db->query('SELECT * FROM test_fetch_func_001'); 15$st->fetchAll( 16 PDO::FETCH_FUNC, 17 function($x, $y) use ($st) { 18 var_dump($st, $x, $y); 19 } 20); 21 22$st = $db->query('SELECT name FROM test_fetch_func_001'); 23var_dump($st->fetchAll(PDO::FETCH_FUNC, 'strtoupper')); 24 25try { 26 $st = $db->query('SELECT * FROM test_fetch_func_001'); 27 var_dump($st->fetchAll(PDO::FETCH_FUNC, 'nothing')); 28} catch (\TypeError $e) { 29 echo $e->getMessage(), \PHP_EOL; 30} 31 32try { 33 $st = $db->query('SELECT * FROM test_fetch_func_001'); 34 var_dump($st->fetchAll(PDO::FETCH_FUNC, '')); 35} catch (\TypeError $e) { 36 echo $e->getMessage(), \PHP_EOL; 37} 38 39try { 40 $st = $db->query('SELECT * FROM test_fetch_func_001'); 41 var_dump($st->fetchAll(PDO::FETCH_FUNC, NULL)); 42} catch (\TypeError $e) { 43 echo $e->getMessage(), \PHP_EOL; 44} 45 46try { 47 $st = $db->query('SELECT * FROM test_fetch_func_001'); 48 var_dump($st->fetchAll(PDO::FETCH_FUNC, 1)); 49} catch (\TypeError $e) { 50 echo $e->getMessage(), \PHP_EOL; 51} 52 53try { 54 $st = $db->query('SELECT * FROM test_fetch_func_001'); 55 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('self', 'foo'))); 56} catch (\TypeError $e) { 57 echo $e->getMessage(), \PHP_EOL; 58} 59 60class foo { 61 public function method($x) { 62 return "--- $x ---"; 63 } 64} 65class bar extends foo { 66 public function __construct($db) { 67 $st = $db->query('SELECT * FROM test_fetch_func_001'); 68 var_dump($st->fetchAll(PDO::FETCH_FUNC, array($this, 'parent::method'))); 69 } 70 71 static public function test1($x, $y) { 72 return $x .'---'. $y; 73 } 74 75 private function test2($x, $y) { 76 return $x; 77 } 78 79 public function test3($x, $y) { 80 return $x .'==='. $y; 81 } 82} 83 84new bar($db); 85 86$st = $db->query('SELECT * FROM test_fetch_func_001'); 87var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test1'))); 88 89try { 90 $st = $db->query('SELECT * FROM test_fetch_func_001'); 91 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test2'))); 92} catch (\TypeError $e) { 93 echo $e->getMessage(), \PHP_EOL; 94} 95 96try { 97 $st = $db->query('SELECT * FROM test_fetch_func_001'); 98 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test3'))); 99} catch (\TypeError $e) { 100 echo $e->getMessage(), \PHP_EOL; 101} 102 103try { 104 $st = $db->query('SELECT * FROM test_fetch_func_001'); 105 var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'inexistent'))); 106} catch (\TypeError $e) { 107 echo $e->getMessage(), \PHP_EOL; 108} 109 110?> 111--EXPECTF-- 112object(PDOStatement)#%d (1) { 113 ["queryString"]=> 114 string(33) "SELECT * FROM test_fetch_func_001" 115} 116int(1) 117string(3) "php" 118object(PDOStatement)#%d (1) { 119 ["queryString"]=> 120 string(33) "SELECT * FROM test_fetch_func_001" 121} 122int(2) 123string(0) "" 124array(2) { 125 [0]=> 126 string(3) "PHP" 127 [1]=> 128 string(0) "" 129} 130function "nothing" not found or invalid function name 131function "" not found or invalid function name 132PDOStatement::fetchAll(): Argument #2 must be a callable, null given 133no array or string given 134cannot access "self" when no class scope is active 135 136Deprecated: Callables of the form ["bar", "parent::method"] are deprecated in %s on line %d 137array(2) { 138 [0]=> 139 string(9) "--- 1 ---" 140 [1]=> 141 string(9) "--- 2 ---" 142} 143array(2) { 144 [0]=> 145 string(7) "1---php" 146 [1]=> 147 string(4) "2---" 148} 149non-static method bar::test2() cannot be called statically 150non-static method bar::test3() cannot be called statically 151class bar does not have a method "inexistent" 152