com php-src: Added support for nullable typed properties: Zend/tests/type_declarations/typed_properties_047 .phpt
Zend/tests/type_declarations/typed_properties_ 048.phpt Zend/zend_API.c Zend/zend_API.h Zend/zend_compile.c Zend/zend_compile.h Zend/zend _execute.c
Zend/zend_inheritance.c ext/standard/var .c
| From: | Bob Weinand | Date: | Tue, 24 May 2016 18:36:52 +0000 |
| Subject: | com php-src: Added support for nullable typed properties: Zend/tests/type_declarations/typed_properties_047 .phpt Zend/tests/type_declarations/typed_properties_ 048.phpt Zend/zend_API.c Zend/zend_API.h Zend/zend_compile.c Zend/zend_compile.h Zend/zend _execute.c Zend/zend_inheritance.c ext/standard/var .c |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-95534@lists.php.net to get a copy of this message | ||
Commit: ff19dec4ea0fc80b5ce394f11b8be4fd5953b5cf
Author: Dmitry Stogov <dmitry@zend.com> Tue, 24 May 2016 21:36:52 +0300
Parents: d46584fef96b151da3f741bface759ee4098a6d4
Branches: typed_ref_properties
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=ff19dec4ea0fc80b5ce394f11b8be4fd5953b5cf
Log:
Added support for nullable typed properties
Changed paths:
A Zend/tests/type_declarations/typed_properties_047.phpt
A Zend/tests/type_declarations/typed_properties_048.phpt
M Zend/zend_API.c
M Zend/zend_API.h
M Zend/zend_compile.c
M Zend/zend_compile.h
M Zend/zend_execute.c
M Zend/zend_inheritance.c
M ext/standard/var.c
diff --git a/Zend/tests/type_declarations/typed_properties_047.phpt b/Zend/tests/type_declarations/typed_properties_047.phpt new file mode 100644 index 0000000..dc59276 --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_047.phpt @@ -0,0 +1,60 @@ +--TEST-- +Nullable typed property +--FILE-- +<?php +class Foo { + public ?int $foo; +} +$x = new Foo(); +var_dump($x); +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = 5; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = null; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + unset($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = "ops"; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +object(Foo)#1 (0) { + ["foo"]=> + uninitialized(?integer) +} +Typed property Foo::$foo must not be accessed before initialization +int(5) +NULL +Typed property Foo::$foo must not be accessed before initialization +Typed property Foo::$foo must be integer, string used diff --git a/Zend/tests/type_declarations/typed_properties_048.phpt b/Zend/tests/type_declarations/typed_properties_048.phpt new file mode 100644 index 0000000..b0a7517 --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_048.phpt @@ -0,0 +1,60 @@ +--TEST-- +Nullable typed property +--FILE-- +<?php +class Foo { + public ?int $foo = null; +} +$x = new Foo(); +var_dump($x); +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = 5; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = null; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + unset($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = "ops"; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +object(Foo)#1 (1) { + ["foo"]=> + NULL +} +NULL +int(5) +NULL +Typed property Foo::$foo must not be accessed before initialization +Typed property Foo::$foo must be integer, string used diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 43cfd4b..d992b95 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -3606,7 +3606,7 @@ ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */ } /* }}} */ -ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar optional_type, zend_string *optional_type_name) /* {{{ */ +ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar optional_type, zend_string *optional_type_name, zend_bool allow_null) /* {{{ */ { zend_property_info *property_info, *property_info_ptr; @@ -3693,6 +3693,7 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name property_info->doc_comment = doc_comment; property_info->ce = ce; property_info->type = optional_type; + property_info->allow_null = allow_null; property_info->type_name = optional_type_name ? zend_new_interned_string(optional_type_name) : NULL; property_info->type_ce = NULL; @@ -3705,7 +3706,7 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */ { - return zend_declare_typed_property(ce, name, property, access_type, doc_comment, 0, NULL); + return zend_declare_typed_property(ce, name, property, access_type, doc_comment, 0, NULL, 0); } /* }}} */ diff --git a/Zend/zend_API.h b/Zend/zend_API.h index 6953d19..805dd89 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -314,7 +314,7 @@ ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_nam ZEND_API const char *zend_get_module_version(const char *module_name); ZEND_API int zend_get_module_started(const char *module_name); -ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar type, zend_string *type_name); +ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar type, zend_string *type_name, zend_bool allow_null); ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment); ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type); diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 2101668..446af60 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -5500,6 +5500,7 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ zend_string *doc_comment = NULL; zval value_zv; zend_uchar optional_type = 0; + zend_bool allow_null = 0; zend_string *optional_type_name = NULL; if (type_ast) { @@ -5516,6 +5517,10 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ zend_string *class_name = zend_ast_get_str(type_ast); zend_uchar type = zend_lookup_builtin_type_by_name(class_name); + if (type_ast->attr & ZEND_TYPE_NULLABLE) { + allow_null = 1; + type_ast->attr &= ~ZEND_TYPE_NULLABLE; + } if (type != 0) { if (type_ast->attr != ZEND_NAME_NOT_FQ) { zend_error_noreturn(E_COMPILE_ERROR, @@ -5568,7 +5573,9 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ zend_const_expr_to_zval(&value_zv, value_ast); if (optional_type && !Z_CONSTANT(value_zv)) { - if (optional_type == IS_ARRAY) { + if (allow_null && Z_TYPE(value_zv) == IS_NULL) { + /* pass */ + } else if (optional_type == IS_ARRAY) { if (Z_TYPE(value_zv) != IS_ARRAY) { zend_error_noreturn(E_COMPILE_ERROR, "Default value for properties with array type can only be an array"); @@ -5594,7 +5601,7 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ name = zend_new_interned_string_safe(name); if (optional_type) { - zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, optional_type, optional_type_name); + zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, optional_type, optional_type_name, allow_null); } else { zend_declare_property_ex(ce, name, &value_zv, flags, doc_comment); } diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index e08f589..2be0371 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -298,6 +298,7 @@ typedef struct _zend_property_info { zend_string *doc_comment; zend_class_entry *ce; zend_uchar type; + zend_bool allow_null; zend_string *type_name; zend_class_entry *type_ce; } zend_property_info; diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 334399c..b2a7ad6 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -860,6 +860,8 @@ static zend_always_inline zend_bool i_zend_verify_property_type(zend_property_in } } return 1; + } else if (info->allow_null && Z_TYPE_P(property) == IS_NULL) { + return 1; } else if (EXPECTED(info->type == IS_CALLABLE)) { if (Z_TYPE_P(property) == IS_OBJECT) { return instanceof_function(zend_ce_closure, Z_OBJCE_P(property)); diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c index d14d190..5ebf38b 100644 --- a/Zend/zend_inheritance.c +++ b/Zend/zend_inheritance.c @@ -711,20 +711,24 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke if (UNEXPECTED(parent_info->type && !(parent_info->flags & ZEND_ACC_PRIVATE))) { if (parent_info->type == IS_OBJECT) { if (child_info->type != IS_OBJECT || + child_info->allow_null != parent_info->allow_null || !zend_string_equals_ci(zend_resolve_property_type(parent_info->type_name, parent_info->ce), zend_resolve_property_type(child_info->type_name, child_info->ce))) { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", ZSTR_VAL(zend_resolve_property_type(parent_info->type_name, parent_info->ce)), ZSTR_VAL(ce->parent->name)); } - } else if (parent_info->type != child_info->type) { + } else if (parent_info->type != child_info->type || + parent_info->allow_null != child_info->allow_null) { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", zend_get_type_by_const(parent_info->type), ZSTR_VAL(ce->parent->name)); } @@ -740,16 +744,18 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke if (UNEXPECTED(parent_info->type && !(parent_info->flags & ZEND_ACC_PRIVATE))) { if (parent_info->type == IS_OBJECT) { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", ZSTR_VAL(zend_resolve_property_type(parent_info->type_name, parent_info->ce)), ZSTR_VAL(ce->parent->name)); } else { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", zend_get_type_by_const(parent_info->type), ZSTR_VAL(ce->parent->name)); } @@ -1675,7 +1681,7 @@ static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */ if (Z_TYPE_P(prop_value) == IS_NULL) { ZVAL_UNDEF(prop_value); } - zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, property_info->type, property_info->type_name); + zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, property_info->type, property_info->type_name, property_info->allow_null); } else { zend_declare_property_ex(ce, prop_name, prop_value, flags, doc_comment); } diff --git a/ext/standard/var.c b/ext/standard/var.c index f5b5786..16e05cd 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -74,8 +74,9 @@ static void php_object_property_dump(zend_property_info *prop_info, zval *zv, ze if (Z_TYPE_P(zv) == IS_UNDEF) { if (prop_info->type) { - php_printf("%*cuninitialized(%s)\n", + php_printf("%*cuninitialized(%s%s)\n", level + 1, ' ', + prop_info->allow_null ? "?" : "", (prop_info->type == IS_OBJECT) ? ZSTR_VAL(prop_info->type_name) : zend_get_type_by_const(prop_info->type)); @@ -269,8 +270,9 @@ static void zval_object_property_dump(zend_property_info *prop_info, zval *zv, z } if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) { if (prop_info->type) { - php_printf("%*cuninitialized(%s)\n", + php_printf("%*cuninitialized(%s%s)\n", level + 1, ' ', + prop_info->allow_null ? "?" : "", (prop_info->type == IS_OBJECT) ? ZSTR_VAL(prop_info->type_name) : zend_get_type_by_const(prop_info->type));
diff --git a/Zend/tests/type_declarations/typed_properties_047.phpt b/Zend/tests/type_declarations/typed_properties_047.phpt new file mode 100644 index 0000000..dc59276 --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_047.phpt @@ -0,0 +1,60 @@ +--TEST-- +Nullable typed property +--FILE-- +<?php +class Foo { + public ?int $foo; +} +$x = new Foo(); +var_dump($x); +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = 5; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = null; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + unset($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = "ops"; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +object(Foo)#1 (0) { + ["foo"]=> + uninitialized(?integer) +} +Typed property Foo::$foo must not be accessed before initialization +int(5) +NULL +Typed property Foo::$foo must not be accessed before initialization +Typed property Foo::$foo must be integer, string used diff --git a/Zend/tests/type_declarations/typed_properties_048.phpt b/Zend/tests/type_declarations/typed_properties_048.phpt new file mode 100644 index 0000000..b0a7517 --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_048.phpt @@ -0,0 +1,60 @@ +--TEST-- +Nullable typed property +--FILE-- +<?php +class Foo { + public ?int $foo = null; +} +$x = new Foo(); +var_dump($x); +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = 5; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = null; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + unset($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + var_dump($x->foo); +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +try { + $x->foo = "ops"; +} catch (Throwable $e) { + echo $e->getMessage()."\n"; +} +?> +--EXPECT-- +object(Foo)#1 (1) { + ["foo"]=> + NULL +} +NULL +int(5) +NULL +Typed property Foo::$foo must not be accessed before initialization +Typed property Foo::$foo must be integer, string used diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 43cfd4b..d992b95 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -3606,7 +3606,7 @@ ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */ } /* }}} */ -ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar optional_type, zend_string *optional_type_name) /* {{{ */ +ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar optional_type, zend_string *optional_type_name, zend_bool allow_null) /* {{{ */ { zend_property_info *property_info, *property_info_ptr; @@ -3693,6 +3693,7 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name property_info->doc_comment = doc_comment; property_info->ce = ce; property_info->type = optional_type; + property_info->allow_null = allow_null; property_info->type_name = optional_type_name ? zend_new_interned_string(optional_type_name) : NULL; property_info->type_ce = NULL; @@ -3705,7 +3706,7 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */ { - return zend_declare_typed_property(ce, name, property, access_type, doc_comment, 0, NULL); + return zend_declare_typed_property(ce, name, property, access_type, doc_comment, 0, NULL, 0); } /* }}} */ diff --git a/Zend/zend_API.h b/Zend/zend_API.h index 6953d19..805dd89 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -314,7 +314,7 @@ ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_nam ZEND_API const char *zend_get_module_version(const char *module_name); ZEND_API int zend_get_module_started(const char *module_name); -ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar type, zend_string *type_name); +ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_uchar type, zend_string *type_name, zend_bool allow_null); ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment); ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type); diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 2101668..446af60 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -5500,6 +5500,7 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ zend_string *doc_comment = NULL; zval value_zv; zend_uchar optional_type = 0; + zend_bool allow_null = 0; zend_string *optional_type_name = NULL; if (type_ast) { @@ -5516,6 +5517,10 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ zend_string *class_name = zend_ast_get_str(type_ast); zend_uchar type = zend_lookup_builtin_type_by_name(class_name); + if (type_ast->attr & ZEND_TYPE_NULLABLE) { + allow_null = 1; + type_ast->attr &= ~ZEND_TYPE_NULLABLE; + } if (type != 0) { if (type_ast->attr != ZEND_NAME_NOT_FQ) { zend_error_noreturn(E_COMPILE_ERROR, @@ -5568,7 +5573,9 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ zend_const_expr_to_zval(&value_zv, value_ast); if (optional_type && !Z_CONSTANT(value_zv)) { - if (optional_type == IS_ARRAY) { + if (allow_null && Z_TYPE(value_zv) == IS_NULL) { + /* pass */ + } else if (optional_type == IS_ARRAY) { if (Z_TYPE(value_zv) != IS_ARRAY) { zend_error_noreturn(E_COMPILE_ERROR, "Default value for properties with array type can only be an array"); @@ -5594,7 +5601,7 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast) /* {{{ */ name = zend_new_interned_string_safe(name); if (optional_type) { - zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, optional_type, optional_type_name); + zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, optional_type, optional_type_name, allow_null); } else { zend_declare_property_ex(ce, name, &value_zv, flags, doc_comment); } diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index e08f589..2be0371 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -298,6 +298,7 @@ typedef struct _zend_property_info { zend_string *doc_comment; zend_class_entry *ce; zend_uchar type; + zend_bool allow_null; zend_string *type_name; zend_class_entry *type_ce; } zend_property_info; diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 334399c..b2a7ad6 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -860,6 +860,8 @@ static zend_always_inline zend_bool i_zend_verify_property_type(zend_property_in } } return 1; + } else if (info->allow_null && Z_TYPE_P(property) == IS_NULL) { + return 1; } else if (EXPECTED(info->type == IS_CALLABLE)) { if (Z_TYPE_P(property) == IS_OBJECT) { return instanceof_function(zend_ce_closure, Z_OBJCE_P(property)); diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c index d14d190..5ebf38b 100644 --- a/Zend/zend_inheritance.c +++ b/Zend/zend_inheritance.c @@ -711,20 +711,24 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke if (UNEXPECTED(parent_info->type && !(parent_info->flags & ZEND_ACC_PRIVATE))) { if (parent_info->type == IS_OBJECT) { if (child_info->type != IS_OBJECT || + child_info->allow_null != parent_info->allow_null || !zend_string_equals_ci(zend_resolve_property_type(parent_info->type_name, parent_info->ce), zend_resolve_property_type(child_info->type_name, child_info->ce))) { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", ZSTR_VAL(zend_resolve_property_type(parent_info->type_name, parent_info->ce)), ZSTR_VAL(ce->parent->name)); } - } else if (parent_info->type != child_info->type) { + } else if (parent_info->type != child_info->type || + parent_info->allow_null != child_info->allow_null) { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", zend_get_type_by_const(parent_info->type), ZSTR_VAL(ce->parent->name)); } @@ -740,16 +744,18 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke if (UNEXPECTED(parent_info->type && !(parent_info->flags & ZEND_ACC_PRIVATE))) { if (parent_info->type == IS_OBJECT) { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", ZSTR_VAL(zend_resolve_property_type(parent_info->type_name, parent_info->ce)), ZSTR_VAL(ce->parent->name)); } else { zend_error_noreturn(E_COMPILE_ERROR, - "Type of %s::$%s must be %s (as in class %s)", + "Type of %s::$%s must be %s%s (as in class %s)", ZSTR_VAL(ce->name), ZSTR_VAL(key), + parent_info->allow_null ? "?" : "", zend_get_type_by_const(parent_info->type), ZSTR_VAL(ce->parent->name)); } @@ -1675,7 +1681,7 @@ static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */ if (Z_TYPE_P(prop_value) == IS_NULL) { ZVAL_UNDEF(prop_value); } - zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, property_info->type, property_info->type_name); + zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, property_info->type, property_info->type_name, property_info->allow_null); } else { zend_declare_property_ex(ce, prop_name, prop_value, flags, doc_comment); } diff --git a/ext/standard/var.c b/ext/standard/var.c index f5b5786..16e05cd 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -74,8 +74,9 @@ static void php_object_property_dump(zend_property_info *prop_info, zval *zv, ze if (Z_TYPE_P(zv) == IS_UNDEF) { if (prop_info->type) { - php_printf("%*cuninitialized(%s)\n", + php_printf("%*cuninitialized(%s%s)\n", level + 1, ' ', + prop_info->allow_null ? "?" : "", (prop_info->type == IS_OBJECT) ? ZSTR_VAL(prop_info->type_name) : zend_get_type_by_const(prop_info->type)); @@ -269,8 +270,9 @@ static void zval_object_property_dump(zend_property_info *prop_info, zval *zv, z } if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) { if (prop_info->type) { - php_printf("%*cuninitialized(%s)\n", + php_printf("%*cuninitialized(%s%s)\n", level + 1, ' ', + prop_info->allow_null ? "?" : "", (prop_info->type == IS_OBJECT) ? ZSTR_VAL(prop_info->type_name) : zend_get_type_by_const(prop_info->type));