Skip to content

Lazy objects: foreach edge cases #15960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions Zend/tests/lazy_objects/foreach_reset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
--TEST--
Lazy Objects: resetAsLazy() during foreach() is not allowed
--FILE--
<?php

class NoHooks {
public int $a = 1;
}

class Hooks {
public int $a = 1;
public int $b { get { return 2; } }
}

function test(string $name, object $obj) {
printf("# %s\n", $name);
$reflector = new ReflectionClass($obj::class);
foreach ($obj as $key => $value) {
var_dump($key);
try {
$reflector->resetAsLazyProxy($obj, function () {});
} catch (Error $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}
}
}

$nohooks = new ReflectionClass(NoHooks::class);
$hooks = new ReflectionClass(Hooks::class);

$obj = $nohooks->newLazyGhost(function () {});

test('Ghost', $obj);

$obj = $hooks->newLazyGhost(function () {});

test('Ghost (hooks)', $obj);

$obj = $nohooks->newLazyProxy(function () {
return new NoHooks();
});

test('Proxy', $obj);

$obj = $hooks->newLazyProxy(function () {
return new Hooks();
});

test('Proxy', $obj);

$obj = new NoHooks();

test('Non lazy', $obj);

$obj = new Hooks();

test('Non lazy (hooks)', $obj);

$obj = new NoHooks();
ob_start();
var_dump($obj);
ob_end_clean();

test('Non lazy with ht', $obj);

$obj = new Hooks();
ob_start();
var_dump($obj);
ob_end_clean();

test('Non lazy with ht (hooks)', $obj);

?>
==DONE==
--EXPECT--
# Ghost
string(1) "a"
Error: Can not reset an object during property iteration
# Ghost (hooks)
string(1) "a"
Error: Can not reset an object during property iteration
string(1) "b"
Error: Can not reset an object during property iteration
# Proxy
string(1) "a"
Error: Can not reset an object during property iteration
# Proxy
string(1) "a"
Error: Can not reset an object during property iteration
string(1) "b"
Error: Can not reset an object during property iteration
# Non lazy
string(1) "a"
Error: Can not reset an object during property iteration
# Non lazy (hooks)
string(1) "a"
Error: Can not reset an object during property iteration
string(1) "b"
Error: Can not reset an object during property iteration
# Non lazy with ht
string(1) "a"
Error: Can not reset an object during property iteration
# Non lazy with ht (hooks)
string(1) "a"
Error: Can not reset an object during property iteration
string(1) "b"
Error: Can not reset an object during property iteration
==DONE==
37 changes: 37 additions & 0 deletions Zend/tests/lazy_objects/gh15823.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
GH-15823: Wrong expectations in zend_lazy_object_get_properties()
--FILE--
<?php

class C {
public int $a = 1;
}

$reflector = new ReflectionClass(C::class);
$calls = 0;
$obj = $reflector->newLazyGhost(function ($obj) use (&$calls) {
if ($calls++ === 0) {
throw new Error("initializer");
}
$obj->a = 2;
});

// Builds properties ht without lazy initialization
var_dump($obj);
try {
// Lazy initialization fails during fetching of properties ht
json_encode($obj);
} catch (Error $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}

var_dump(json_encode($obj));

?>
--EXPECTF--
lazy ghost object(C)#%d (0) {
["a"]=>
uninitialized(int)
}
Error: initializer
string(7) "{"a":2}"
111 changes: 110 additions & 1 deletion Zend/tests/lazy_objects/init_trigger_foreach.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ Lazy objects: Foreach initializes object

class C {
public int $a;
public int $b;
public function __construct() {
var_dump(__METHOD__);
$this->a = 1;
$this->b = 2;
}
}

Expand All @@ -24,6 +26,17 @@ foreach ($obj as $prop => $value) {
var_dump($prop, $value);
}

print "# Ghost (by ref):\n";

$obj = $reflector->newLazyGhost(function ($obj) {
var_dump("initializer");
$obj->__construct();
});

foreach ($obj as $prop => &$value) {
var_dump($prop, $value);
}

print "# Proxy:\n";

$obj = $reflector->newLazyProxy(function ($obj) {
Expand All @@ -35,14 +48,110 @@ foreach ($obj as $prop => $value) {
var_dump($prop, $value);
}

--EXPECTF--
print "# Proxy (by ref):\n";

$obj = $reflector->newLazyProxy(function ($obj) {
var_dump("initializer");
return new C();
});

foreach ($obj as $prop => &$value) {
var_dump($prop, $value);
}

print "# Ghost (init failure)\n";

$fail = true;
$obj = $reflector->newLazyGhost(function ($obj) use (&$fail) {
if ($fail) {
throw new Exception("initializer");
} else {
var_dump("initializer");
$obj->__construct();
}
});

try {
foreach ($obj as $prop => $value) {
var_dump($prop, $value);
}
} catch (Exception $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}

$fail = false;
foreach ($obj as $prop => $value) {
var_dump($prop, $value);
}

print "# Ghost (init failure, by ref)\n";

$fail = true;
$obj = $reflector->newLazyGhost(function ($obj) use (&$fail) {
if ($fail) {
throw new Exception("initializer");
} else {
var_dump("initializer");
$obj->__construct();
}
});

try {
foreach ($obj as $prop => &$value) {
var_dump($prop, $value);
}
} catch (Exception $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}

$fail = false;
foreach ($obj as $prop => &$value) {
var_dump($prop, $value);
}

?>
--EXPECT--
# Ghost:
string(11) "initializer"
string(14) "C::__construct"
string(1) "a"
int(1)
string(1) "b"
int(2)
# Ghost (by ref):
string(11) "initializer"
string(14) "C::__construct"
string(1) "a"
int(1)
string(1) "b"
int(2)
# Proxy:
string(11) "initializer"
string(14) "C::__construct"
string(1) "a"
int(1)
string(1) "b"
int(2)
# Proxy (by ref):
string(11) "initializer"
string(14) "C::__construct"
string(1) "a"
int(1)
string(1) "b"
int(2)
# Ghost (init failure)
Exception: initializer
string(11) "initializer"
string(14) "C::__construct"
string(1) "a"
int(1)
string(1) "b"
int(2)
# Ghost (init failure, by ref)
Exception: initializer
string(11) "initializer"
string(14) "C::__construct"
string(1) "a"
int(1)
string(1) "b"
int(2)
Loading
Loading