Skip to content

Add support for defining object constants via define() #7243

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 1 commit 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
19 changes: 16 additions & 3 deletions Zend/tests/constexpr/new_allowed.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ function test($param = new stdClass) {
var_dump($param, $var);
}

class Test3 implements Stringable {
public function __toString(): string {
return "hello";
}
}

const TEST = new stdClass;
define("TEST2", new stdClass);
define("TEST3", new Test3);

new Test;
test();
var_dump(TEST);
var_dump(TEST2);
var_dump(TEST3);

?>
--EXPECT--
object(stdClass)#3 (0) {
}
object(stdClass)#2 (0) {
object(stdClass)#4 (0) {
}
object(stdClass)#3 (0) {
}
object(stdClass)#4 (0) {
}
object(stdClass)#1 (0) {
}
object(stdClass)#2 (0) {
}
string(5) "hello"
20 changes: 11 additions & 9 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,6 @@ static bool validate_constant_array_argument(HashTable *ht, int argument_number)
break;
}
}
} else if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_RESOURCE) {
zend_argument_type_error(argument_number, "cannot be an object, %s given", zend_zval_type_name(val));
ret = 0;
break;
}
}
} ZEND_HASH_FOREACH_END();
Expand All @@ -444,6 +440,13 @@ static bool validate_constant_array_argument(HashTable *ht, int argument_number)
}
/* }}} */

static bool validate_constant_obj_argument(zend_object *obj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearly, this is just a placeholder :( I'm not sure what should be validated

{
bool ret = 1;

return ret;
}

static void copy_constant_array(zval *dst, zval *src) /* {{{ */
{
zend_string *key;
Expand Down Expand Up @@ -520,11 +523,10 @@ ZEND_FUNCTION(define)
val = &val_free;
break;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if above seems a bit problematic, since either BC or consistency is harmed, either it is removed, or left as-is.

ZEND_FALLTHROUGH;
default:
zval_ptr_dtor(&val_free);
zend_argument_type_error(2, "cannot be an object, %s given", zend_zval_type_name(val));
RETURN_THROWS();

if (!validate_constant_obj_argument(Z_OBJ_P(val))) {
RETURN_THROWS();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure we need some custom code here, just like arrays do

}

ZVAL_COPY(&c.value, val);
Expand Down
3 changes: 1 addition & 2 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ function strncasecmp(string $string1, string $string2, int $length): int {}

function error_reporting(?int $error_level = null): int {}

/** @param mixed $value */
function define(string $constant_name, $value, bool $case_insensitive = false): bool {}
function define(string $constant_name, mixed $value, bool $case_insensitive = false): bool {}

function defined(string $constant_name): bool {}

Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_builtin_functions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: c333499e3ea100d976f0fa8bb8800ed21b04f1c6 */
* Stub hash: 38936b472d60d9eeb2cc8e35c1276e9f707837bf */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_version, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -39,7 +39,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_define, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, constant_name, IS_STRING, 0)
ZEND_ARG_INFO(0, value)
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, case_insensitive, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()

Expand Down