com php-src: Check type after constant resolution: Zend/tests/t ype_declarations/typed_properties_058.phpt Zend /zend_API.c
| From: | Bob Weinand | Date: | Wed, 25 May 2016 20:02:23 +0000 |
| Subject: | com php-src: Check type after constant resolution: Zend/tests/t ype_declarations/typed_properties_058.phpt Zend /zend_API.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-95510@lists.php.net to get a copy of this message | ||
Commit: 2019f660d47b3b034a4485f0ff8c3a02b800409d
Author: Dmitry Stogov <dmitry@zend.com> Wed, 25 May 2016 23:02:23 +0300
Parents: b0dcf0c82aaace45723e15375e3d5070965ce226
Branches: typed_ref_properties
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=2019f660d47b3b034a4485f0ff8c3a02b800409d
Log:
Check type after constant resolution
Changed paths:
A Zend/tests/type_declarations/typed_properties_058.phpt
M Zend/zend_API.c
Diff:
diff --git a/Zend/tests/type_declarations/typed_properties_058.phpt
b/Zend/tests/type_declarations/typed_properties_058.phpt
new file mode 100644
index 0000000..18fae53
--- /dev/null
+++ b/Zend/tests/type_declarations/typed_properties_058.phpt
@@ -0,0 +1,33 @@
+--TEST--
+Constants in default values of properties
+--FILE--
+<?php
+declare(strict_types=1);
+
+define("FOO", 5);
+
+class A {
+ public int $foo = FOO;
+}
+
+class B {
+ public string $foo = FOO;
+}
+
+try {
+ $o = new A();
+ var_dump($o->foo);
+} catch (Throwable $e) {
+ echo $e->getMessage() . "\n";
+}
+
+try {
+ $o = new B();
+ var_dump($o->foo);
+} catch (Throwable $e) {
+ echo $e->getMessage() . "\n";
+}
+?>
+--EXPECT--
+int(5)
+Typed property B::$foo must be string, integer used
diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index 73db276..799d462 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -1147,6 +1147,17 @@ ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{
*/
if (UNEXPECTED(zval_update_constant_ex(val, ce) != SUCCESS)) {
return FAILURE;
}
+ if (prop_info->type) {
+ zval tmp, *v;
+
+ v = zend_verify_property_type(prop_info, val, &tmp,
ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)));
+ if (UNEXPECTED(!v)) {
+ zend_verify_property_type_error(prop_info, prop_info->name, val);
+ return FAILURE;
+ } else if (val != v) {
+ ZVAL_COPY_VALUE(val, v);
+ }
+ }
}
}
} ZEND_HASH_FOREACH_END();