-
Notifications
You must be signed in to change notification settings - Fork 7.9k
RFC: Clone with v2 #18747
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
Merged
+701
−10
Merged
RFC: Clone with v2 #18747
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b2758a3
zend_objects: Add `clone_obj_with` object handler
TimWolla 4f7b47d
zend_objects: Use the `write_property()` handler directly in `zend_ob…
TimWolla 053bd89
zend_objects: Unlock readonly properties after `__clone()` for clone-…
TimWolla be7c8b6
ext/com_dotnet: Fix object handlers
TimWolla 71d8ff5
zend_builtin_functions: Add `withProperties` parameter to `clone()` f…
TimWolla 2f5e9eb
zend_vm: Use the `clone_obj_with` handler in ZEND_CLONE
TimWolla 6d9c0c6
Zend: Implement the default clone_with as clone + manual property adj…
TimWolla fde4546
zend_objects: Disallow clone_with by reference
TimWolla fda5e04
NEWS / UPGRADING
TimWolla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--TEST-- | ||
Clone with basic | ||
--FILE-- | ||
<?php | ||
|
||
class Dummy { } | ||
|
||
$x = new stdClass(); | ||
|
||
$foo = 'FOO'; | ||
$bar = new Dummy(); | ||
$array = [ | ||
'baz' => 'BAZ', | ||
'array' => [1, 2, 3], | ||
]; | ||
|
||
var_dump(clone $x); | ||
var_dump(clone($x)); | ||
var_dump(clone($x, [ 'foo' => $foo, 'bar' => $bar ])); | ||
var_dump(clone($x, $array)); | ||
var_dump(clone($x, [ 'obj' => $x ])); | ||
|
||
var_dump(clone($x, [ | ||
'abc', | ||
'def', | ||
new Dummy(), | ||
'named' => 'value', | ||
])); | ||
|
||
?> | ||
--EXPECTF-- | ||
object(stdClass)#%d (0) { | ||
} | ||
object(stdClass)#%d (0) { | ||
} | ||
object(stdClass)#%d (2) { | ||
["foo"]=> | ||
string(3) "FOO" | ||
["bar"]=> | ||
object(Dummy)#%d (0) { | ||
} | ||
} | ||
object(stdClass)#%d (2) { | ||
["baz"]=> | ||
string(3) "BAZ" | ||
["array"]=> | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
[2]=> | ||
int(3) | ||
} | ||
} | ||
object(stdClass)#%d (1) { | ||
["obj"]=> | ||
object(stdClass)#%d (0) { | ||
} | ||
} | ||
object(stdClass)#%d (4) { | ||
["0"]=> | ||
string(3) "abc" | ||
["1"]=> | ||
string(3) "def" | ||
["2"]=> | ||
object(Dummy)#%d (0) { | ||
} | ||
["named"]=> | ||
string(5) "value" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
--TEST-- | ||
Clone with respects visiblity | ||
--FILE-- | ||
<?php | ||
|
||
class P { | ||
public $a = 'default'; | ||
protected $b = 'default'; | ||
private $c = 'default'; | ||
public private(set) string $d = 'default'; | ||
|
||
public function m1() { | ||
return clone($this, [ 'a' => 'updated A', 'b' => 'updated B', 'c' => 'updated C', 'd' => 'updated D' ]); | ||
} | ||
} | ||
|
||
class C extends P { | ||
public function m2() { | ||
return clone($this, [ 'a' => 'updated A', 'b' => 'updated B', 'c' => 'dynamic C' ]); | ||
} | ||
|
||
public function m3() { | ||
return clone($this, [ 'd' => 'inaccessible' ]); | ||
} | ||
} | ||
|
||
class Unrelated { | ||
public function m3(P $p) { | ||
return clone($p, [ 'b' => 'inaccessible' ]); | ||
} | ||
} | ||
|
||
$p = new P(); | ||
|
||
var_dump(clone($p, [ 'a' => 'updated A' ])); | ||
var_dump($p->m1()); | ||
|
||
$c = new C(); | ||
var_dump($c->m1()); | ||
var_dump($c->m2()); | ||
try { | ||
var_dump($c->m3()); | ||
} catch (Error $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
try { | ||
var_dump(clone($p, [ 'b' => 'inaccessible' ])); | ||
} catch (Error $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
try { | ||
var_dump(clone($p, [ 'd' => 'inaccessible' ])); | ||
} catch (Error $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
try { | ||
var_dump((new Unrelated())->m3($p)); | ||
} catch (Error $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
object(P)#%d (4) { | ||
["a"]=> | ||
string(9) "updated A" | ||
["b":protected]=> | ||
string(7) "default" | ||
["c":"P":private]=> | ||
string(7) "default" | ||
["d"]=> | ||
string(7) "default" | ||
} | ||
object(P)#%d (4) { | ||
["a"]=> | ||
string(9) "updated A" | ||
["b":protected]=> | ||
string(9) "updated B" | ||
["c":"P":private]=> | ||
string(9) "updated C" | ||
["d"]=> | ||
string(9) "updated D" | ||
} | ||
object(C)#%d (4) { | ||
["a"]=> | ||
string(9) "updated A" | ||
["b":protected]=> | ||
string(9) "updated B" | ||
["c":"P":private]=> | ||
string(9) "updated C" | ||
["d"]=> | ||
string(9) "updated D" | ||
} | ||
|
||
Deprecated: Creation of dynamic property C::$c is deprecated in %s on line %d | ||
object(C)#%d (5) { | ||
["a"]=> | ||
string(9) "updated A" | ||
["b":protected]=> | ||
string(9) "updated B" | ||
["c":"P":private]=> | ||
string(7) "default" | ||
["d"]=> | ||
string(7) "default" | ||
["c"]=> | ||
string(9) "dynamic C" | ||
} | ||
Error: Cannot modify private(set) property P::$d from scope C | ||
Error: Cannot access protected property P::$b | ||
Error: Cannot modify private(set) property P::$d from global scope | ||
Error: Cannot access protected property P::$b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Clone with supports property hooks | ||
--FILE-- | ||
<?php | ||
|
||
class Clazz { | ||
public string $hooked = 'default' { | ||
set { | ||
$this->hooked = strtoupper($value); | ||
} | ||
} | ||
} | ||
|
||
$c = new Clazz(); | ||
|
||
var_dump(clone($c, [ 'hooked' => 'updated' ])); | ||
|
||
?> | ||
--EXPECTF-- | ||
object(Clazz)#%d (1) { | ||
["hooked"]=> | ||
string(7) "UPDATED" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--TEST-- | ||
Clone with evaluation order | ||
--FILE-- | ||
<?php | ||
|
||
class Clazz { | ||
public string $hooked = 'default' { | ||
set { | ||
echo __FUNCTION__, PHP_EOL; | ||
|
||
$this->hooked = strtoupper($value); | ||
} | ||
} | ||
|
||
public string $maxLength { | ||
set { | ||
echo __FUNCTION__, PHP_EOL; | ||
|
||
if (strlen($value) > 5) { | ||
throw new \Exception('Length exceeded'); | ||
} | ||
|
||
$this->maxLength = $value; | ||
} | ||
} | ||
|
||
public string $minLength { | ||
set { | ||
echo __FUNCTION__, PHP_EOL; | ||
|
||
if (strlen($value) < 5) { | ||
throw new \Exception('Length unsufficient'); | ||
} | ||
|
||
$this->minLength = $value; | ||
} | ||
} | ||
} | ||
|
||
$c = new Clazz(); | ||
|
||
var_dump(clone($c, [ 'hooked' => 'updated' ])); | ||
echo PHP_EOL; | ||
var_dump(clone($c, [ 'hooked' => 'updated', 'maxLength' => 'abc', 'minLength' => 'abcdef' ])); | ||
echo PHP_EOL; | ||
var_dump(clone($c, [ 'minLength' => 'abcdef', 'hooked' => 'updated', 'maxLength' => 'abc' ])); | ||
|
||
?> | ||
--EXPECTF-- | ||
$hooked::set | ||
object(Clazz)#%d (1) { | ||
["hooked"]=> | ||
string(7) "UPDATED" | ||
["maxLength"]=> | ||
uninitialized(string) | ||
["minLength"]=> | ||
uninitialized(string) | ||
} | ||
|
||
$hooked::set | ||
$maxLength::set | ||
$minLength::set | ||
object(Clazz)#%d (3) { | ||
["hooked"]=> | ||
string(7) "UPDATED" | ||
["maxLength"]=> | ||
string(3) "abc" | ||
["minLength"]=> | ||
string(6) "abcdef" | ||
} | ||
|
||
$minLength::set | ||
$hooked::set | ||
$maxLength::set | ||
object(Clazz)#%d (3) { | ||
["hooked"]=> | ||
string(7) "UPDATED" | ||
["maxLength"]=> | ||
string(3) "abc" | ||
["minLength"]=> | ||
string(6) "abcdef" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--TEST-- | ||
Clone with error handling | ||
--FILE-- | ||
<?php | ||
|
||
class Clazz { | ||
public string $hooked = 'default' { | ||
set { | ||
echo __FUNCTION__, PHP_EOL; | ||
|
||
$this->hooked = strtoupper($value); | ||
} | ||
} | ||
|
||
public string $maxLength { | ||
set { | ||
echo __FUNCTION__, PHP_EOL; | ||
|
||
if (strlen($value) > 5) { | ||
throw new \Exception('Length exceeded'); | ||
} | ||
|
||
$this->maxLength = $value; | ||
} | ||
} | ||
|
||
public string $minLength { | ||
set { | ||
echo __FUNCTION__, PHP_EOL; | ||
|
||
if (strlen($value) < 5) { | ||
throw new \Exception('Length insufficient'); | ||
} | ||
|
||
$this->minLength = $value; | ||
} | ||
} | ||
} | ||
|
||
$c = new Clazz(); | ||
|
||
try { | ||
var_dump(clone($c, [ 'hooked' => 'updated', 'maxLength' => 'abcdef', 'minLength' => 'abc' ])); | ||
} catch (Throwable $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
echo PHP_EOL; | ||
|
||
try { | ||
var_dump(clone($c, [ 'hooked' => 'updated', 'minLength' => 'abc', 'maxLength' => 'abcdef' ])); | ||
} catch (Throwable $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
$hooked::set | ||
$maxLength::set | ||
Exception: Length exceeded | ||
|
||
$hooked::set | ||
$minLength::set | ||
Exception: Length insufficient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Clone with error cases | ||
--FILE-- | ||
<?php | ||
|
||
$x = new stdClass(); | ||
|
||
try { | ||
var_dump(clone($x, 1)); | ||
} catch (Throwable $e) { | ||
echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
TypeError: clone(): Argument #2 ($withProperties) must be of type array, int given |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.