Skip to content

RFC: Turn clone() into a function #18919

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
merged 14 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
zend_builtin_functions: Add clone() as function
  • Loading branch information
TimWolla committed Jun 23, 2025
commit 24c0ce71f621bcf1b2543dfd1a843593dd63f33a
43 changes: 43 additions & 0 deletions Zend/tests/clone/clone_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Clone as a function.
--FILE--
<?php

$x = new stdClass();

\var_dump(\clone($x));
\var_dump(\array_map('clone', [$x, $x, $x]));

class Foo {
private function __clone() {

}

public function clone_me() {
// Verify visibility when going through array_map().
return array_map(\clone(...), [$this]);
}
}

$f = new Foo();

$clone = $f->clone_me()[0];

var_dump($f !== $clone);

?>
--EXPECTF--
object(stdClass)#%d (0) {
}
array(3) {
[0]=>
object(stdClass)#%d (0) {
}
[1]=>
object(stdClass)#%d (0) {
}
[2]=>
object(stdClass)#%d (0) {
}
}
bool(true)
1 change: 1 addition & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -3641,6 +3641,7 @@ static void zend_disable_function(const char *function_name, size_t function_nam
if (UNEXPECTED(
(function_name_length == strlen("exit") && !memcmp(function_name, "exit", strlen("exit")))
|| (function_name_length == strlen("die") && !memcmp(function_name, "die", strlen("die")))
|| (function_name_length == strlen("clone") && !memcmp(function_name, "clone", strlen("clone")))
)) {
zend_error(E_WARNING, "Cannot disable function %s()", function_name);
return;
Expand Down
46 changes: 46 additions & 0 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ zend_result zend_startup_builtin_functions(void) /* {{{ */
}
/* }}} */

ZEND_FUNCTION(clone)
{
zend_object *zobj;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ(zobj)
ZEND_PARSE_PARAMETERS_END();

zend_class_entry *scope = zend_get_executed_scope();

zend_class_entry *ce = zobj->ce;
zend_function *clone = ce->clone;

if (UNEXPECTED(zobj->handlers->clone_obj == NULL)) {
zend_throw_error(NULL, "Trying to clone an uncloneable object of class %s", ZSTR_VAL(ce->name));
RETURN_THROWS();
}

if (clone && !(clone->common.fn_flags & ZEND_ACC_PUBLIC)) {
if (clone->common.scope != scope) {
if (UNEXPECTED(clone->common.fn_flags & ZEND_ACC_PRIVATE)
|| UNEXPECTED(!zend_check_protected(zend_get_function_root_class(clone), scope))) {
zend_throw_error(NULL, "Call to %s %s::__clone() from %s%s",
zend_visibility_string(clone->common.fn_flags), ZSTR_VAL(clone->common.scope->name),
scope ? "scope " : "global scope",
scope ? ZSTR_VAL(scope->name) : ""
);
RETURN_THROWS();
}
}
}

zend_object *cloned;
cloned = zobj->handlers->clone_obj(zobj);

if (EG(exception)) {
if (cloned) {
OBJ_RELEASE(cloned);
}

RETURN_THROWS();
}

RETURN_OBJ(cloned);
}

ZEND_FUNCTION(exit)
{
zend_string *str = NULL;
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class stdClass
{
}

function _clone(object $object): object {}

function exit(string|int $status = 0): never {}

/** @alias exit */
Expand Down
8 changes: 7 additions & 1 deletion Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,9 @@ class FunctionName implements FunctionOrMethodName {
private /* readonly */ Name $name;

public function __construct(Name $name) {
if ($name->name === '_clone') {
$name = new Name('clone', $name->getAttributes());
}
$this->name = $name;
}

Expand Down