1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00
Files
archived-php-src/ext/pdo/tests/pdo_fetch_class_basic.phpt
Gina Peter Banyard 3ff7758bcf ext/pdo: Refactor PDO::FETCH_CLASS to not rely on a FCI and use a HashTable for ctor_arg
To call the constructor we now only store the CE and a HashTable for the arguments.
This reduces the size of the _pdo_stmt_t struct from 320 bytes to 232 bytes.
Moreover, this now means that the constructor argument array follows the usual CUFA semantics.
This change is a BC break, as string keys now act like named arguments.
Moreover, the automatic wrapping of by-value arguments for by-ref parameters has been dropped, and the usual E_WARNING is now emitted in those cases.

The do_fetch() is heavily refactored to simplify the execution flow, which also makes it easier to understand.
Additionally we add a new bitflag in_fetch to prevent modification of the fetch flags by userland when PDO is fetching from the DB.
2025-01-30 18:48:54 +00:00

164 lines
3.1 KiB
PHP

--TEST--
PDO Common: PDO::FETCH_CLASS
--EXTENSIONS--
pdo
--SKIPIF--
<?php
$dir = getenv('REDIR_TEST_DIR');
if (false == $dir) die('skip no driver');
require_once $dir . 'pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
$db = PDOTest::factory();
$db->exec('CREATE TABLE test005(id int NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(10))');
$db->exec("INSERT INTO test005 VALUES(1, 'A', 'AA')");
$db->exec("INSERT INTO test005 VALUES(2, 'B', 'BB')");
$db->exec("INSERT INTO test005 VALUES(3, 'C', 'CC')");
$stmt = $db->prepare('SELECT id, val, val2 from test005');
class TestBase
{
public $id;
protected $val;
private $val2;
}
#[AllowDynamicProperties] // $val2 will be dynamic now.
class TestDerived extends TestBase
{
protected $row;
public function __construct(&$row)
{
echo __METHOD__ . "($row,{$this->id})\n";
$this->row = $row++;
}
}
$stmt->execute();
var_dump($stmt->fetchAll(PDO::FETCH_CLASS));
$stmt->execute();
var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'TestBase'));
$stmt->execute();
$rowCounter = 0;
var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'TestDerived', [&$rowCounter]));
?>
--CLEAN--
<?php
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
$db = PDOTest::factory();
PDOTest::dropTableIfExists($db, "test005");
?>
--EXPECTF--
array(3) {
[0]=>
object(stdClass)#%d (3) {
["id"]=>
string(1) "1"
["val"]=>
string(1) "A"
["val2"]=>
string(2) "AA"
}
[1]=>
object(stdClass)#%d (3) {
["id"]=>
string(1) "2"
["val"]=>
string(1) "B"
["val2"]=>
string(2) "BB"
}
[2]=>
object(stdClass)#%d (3) {
["id"]=>
string(1) "3"
["val"]=>
string(1) "C"
["val2"]=>
string(2) "CC"
}
}
array(3) {
[0]=>
object(TestBase)#%d (3) {
["id"]=>
string(1) "1"
["val":protected]=>
string(1) "A"
["val2":"TestBase":private]=>
string(2) "AA"
}
[1]=>
object(TestBase)#%d (3) {
["id"]=>
string(1) "2"
["val":protected]=>
string(1) "B"
["val2":"TestBase":private]=>
string(2) "BB"
}
[2]=>
object(TestBase)#%d (3) {
["id"]=>
string(1) "3"
["val":protected]=>
string(1) "C"
["val2":"TestBase":private]=>
string(2) "CC"
}
}
TestDerived::__construct(0,1)
TestDerived::__construct(1,2)
TestDerived::__construct(2,3)
array(3) {
[0]=>
object(TestDerived)#%d (5) {
["id"]=>
string(1) "1"
["val":protected]=>
string(1) "A"
["val2":"TestBase":private]=>
NULL
["row":protected]=>
int(0)
["val2"]=>
string(2) "AA"
}
[1]=>
object(TestDerived)#%d (5) {
["id"]=>
string(1) "2"
["val":protected]=>
string(1) "B"
["val2":"TestBase":private]=>
NULL
["row":protected]=>
int(1)
["val2"]=>
string(2) "BB"
}
[2]=>
object(TestDerived)#%d (5) {
["id"]=>
string(1) "3"
["val":protected]=>
string(1) "C"
["val2":"TestBase":private]=>
NULL
["row":protected]=>
int(2)
["val2"]=>
string(2) "CC"
}
}