Skip to content

Commit ca49a7b

Browse files
TimWollaedorian
andauthored
RFC: Turn clone() into a function (#18919)
RFC: https://wiki.php.net/rfc/clone_with_v2 Co-authored-by: Volker Dusch <[email protected]>
1 parent 5ed8b2b commit ca49a7b

25 files changed

+227
-58
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PHP NEWS
5959
. Added support for `final` with constructor property promotion.
6060
(DanielEScherzer)
6161
. Do not use RTLD_DEEPBIND if dlmopen is available. (Daniil Gentili)
62+
. Make `clone() a function. (timwolla, edorian)
6263

6364
- Curl:
6465
. Added curl_multi_get_handles(). (timwolla)

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ PHP 8.5 UPGRADE NOTES
374374
. get_exception_handler() allows retrieving the current user-defined exception
375375
handler function.
376376
RFC: https://wiki.php.net/rfc/get-error-exception-handler
377+
. The clone language construct is now a function.
378+
RFC: https://wiki.php.net/rfc/clone_with_v2
377379

378380
- Curl:
379381
. curl_multi_get_handles() allows retrieving all CurlHandles current

Zend/Optimizer/zend_func_infos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* This is a generated file, edit the .stub.php files instead. */
22

33
static const func_info_t func_infos[] = {
4+
F1("clone", MAY_BE_OBJECT),
45
F1("zend_version", MAY_BE_STRING),
56
FN("func_get_args", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY),
67
F1("get_class_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),

Zend/tests/assert/expect_015.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ assert(0 && ($a = function () {
183183
$x = $a ?? $b;
184184
[$a, $b, $c] = [1, 2 => 'x', 'z' => 'c'];
185185
@foo();
186-
$y = clone $x;
186+
$y = \clone($x);
187187
yield 1 => 2;
188188
yield from $x;
189189
}))

Zend/tests/clone/ast.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Ast Printing
3+
--FILE--
4+
<?php
5+
6+
$x = new stdClass();
7+
8+
9+
try {
10+
assert(false && $y = clone $x);
11+
} catch (Error $e) {
12+
echo $e->getMessage(), PHP_EOL;
13+
}
14+
15+
try {
16+
assert(false && $y = clone($x));
17+
} catch (Error $e) {
18+
echo $e->getMessage(), PHP_EOL;
19+
}
20+
21+
try {
22+
assert(false && $y = clone(...));
23+
} catch (Error $e) {
24+
echo $e->getMessage(), PHP_EOL;
25+
}
26+
27+
?>
28+
--EXPECT--
29+
assert(false && ($y = \clone($x)))
30+
assert(false && ($y = \clone($x)))
31+
assert(false && ($y = \clone(...)))

Zend/tests/clone/bug36071.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Bug #36071 (Engine Crash related with 'clone')
44
error_reporting=4095
55
--FILE--
66
<?php
7-
$a = clone 0;
8-
$a[0]->b = 0;
7+
try {
8+
$a = clone 0;
9+
} catch (Error $e) {
10+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
11+
}
912
?>
10-
--EXPECTF--
11-
Fatal error: Uncaught Error: __clone method called on non-object in %sbug36071.php:2
12-
Stack trace:
13-
#0 {main}
14-
thrown in %sbug36071.php on line 2
13+
--EXPECT--
14+
TypeError: clone(): Argument #1 ($object) must be of type object, int given

Zend/tests/clone/bug42817.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Bug #42817 (clone() on a non-object does not result in a fatal error)
33
--FILE--
44
<?php
5-
$a = clone(null);
6-
array_push($a->b, $c);
5+
try {
6+
$a = clone(null);
7+
} catch (Error $e) {
8+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
9+
}
710
?>
8-
--EXPECTF--
9-
Fatal error: Uncaught Error: __clone method called on non-object in %sbug42817.php:2
10-
Stack trace:
11-
#0 {main}
12-
thrown in %sbug42817.php on line 2
11+
--EXPECT--
12+
TypeError: clone(): Argument #1 ($object) must be of type object, null given

Zend/tests/clone/bug42818.phpt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
Bug #42818 ($foo = clone(array()); leaks memory)
33
--FILE--
44
<?php
5-
$foo = clone(array());
5+
try {
6+
$foo = clone(array());
7+
} catch (Error $e) {
8+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
9+
}
610
?>
7-
--EXPECTF--
8-
Fatal error: Uncaught Error: __clone method called on non-object in %sbug42818.php:2
9-
Stack trace:
10-
#0 {main}
11-
thrown in %sbug42818.php on line 2
11+
--EXPECT--
12+
TypeError: clone(): Argument #1 ($object) must be of type object, array given

Zend/tests/clone/clone_001.phpt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ Using clone statement on non-object
33
--FILE--
44
<?php
55

6-
$a = clone array();
6+
try {
7+
$a = clone array();
8+
} catch (Error $e) {
9+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
10+
}
711

812
?>
9-
--EXPECTF--
10-
Fatal error: Uncaught Error: __clone method called on non-object in %s:%d
11-
Stack trace:
12-
#0 {main}
13-
thrown in %s on line %d
13+
--EXPECT--
14+
TypeError: clone(): Argument #1 ($object) must be of type object, array given

Zend/tests/clone/clone_003.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ Using clone statement on undefined variable
33
--FILE--
44
<?php
55

6-
$a = clone $b;
6+
try {
7+
$a = clone $b;
8+
} catch (Error $e) {
9+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
10+
}
711

812
?>
913
--EXPECTF--
1014
Warning: Undefined variable $b in %s on line %d
11-
12-
Fatal error: Uncaught Error: __clone method called on non-object in %s:%d
13-
Stack trace:
14-
#0 {main}
15-
thrown in %s on line %d
15+
TypeError: clone(): Argument #1 ($object) must be of type object, null given

0 commit comments

Comments
 (0)