diff --git a/Zend/tests/exceptions/exception_009.phpt b/Zend/tests/exceptions/exception_009.phpt index 340bd99eb0c2..bdabe46f6fda 100644 --- a/Zend/tests/exceptions/exception_009.phpt +++ b/Zend/tests/exceptions/exception_009.phpt @@ -24,7 +24,9 @@ class my_exception extends exception throw new my_exception; ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from my_file to string for property Exception::$file of type string is deprecated in %s on line %d + Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in [no active file]:0 Stack trace: #0 [internal function]: Exception->__toString() diff --git a/Zend/tests/gh10169.phpt b/Zend/tests/gh10169.phpt index 44f62b7dcd53..c4dee2ed776b 100644 --- a/Zend/tests/gh10169.phpt +++ b/Zend/tests/gh10169.phpt @@ -32,6 +32,9 @@ try { } ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from B to string for property A::$prop of type string is deprecated in %s on line %d Error: Object was released while assigning to property A::$prop + +Deprecated: Implicit conversion from B to string for property A::$prop of type string is deprecated in %s on line %d Error: Object was released while assigning to property A::$prop diff --git a/Zend/tests/lazy_objects/setRawValueWithoutLazyInitialization_side_effect_toString.phpt b/Zend/tests/lazy_objects/setRawValueWithoutLazyInitialization_side_effect_toString.phpt index 1c8eac82880c..aeb0ea1505e4 100644 --- a/Zend/tests/lazy_objects/setRawValueWithoutLazyInitialization_side_effect_toString.phpt +++ b/Zend/tests/lazy_objects/setRawValueWithoutLazyInitialization_side_effect_toString.phpt @@ -46,6 +46,8 @@ test('Proxy', $obj); ?> --EXPECTF-- # Ghost + +Deprecated: Implicit conversion from class@anonymous to string for property C::$a of type string is deprecated in %s on line %d C::__construct bool(true) object(C)#%d (2) { @@ -55,6 +57,8 @@ object(C)#%d (2) { string(1) "b" } # Proxy + +Deprecated: Implicit conversion from class@anonymous to string for property C::$a of type string is deprecated in %s on line %d C::__construct bool(true) lazy proxy object(C)#%d (1) { diff --git a/Zend/tests/list/list_from_array_literal_order.phpt b/Zend/tests/list/list_from_array_literal_order.phpt index db8f9e71d54a..651e76a648f9 100644 --- a/Zend/tests/list/list_from_array_literal_order.phpt +++ b/Zend/tests/list/list_from_array_literal_order.phpt @@ -4,7 +4,7 @@ List assignment from an array literal: evaluation order and lifetime of the valu ---EXPECT-- +--EXPECTF-- -- values before targets value 1 value 2 @@ -47,6 +47,8 @@ boom int(0) -- a variable that is a reference to a typed property construct coerced + +Deprecated: Implicit conversion from D to string for reference held by property S::$s of type string is deprecated in %s on line %d destruct coerced set y assigned diff --git a/Zend/tests/list/list_from_array_literal_order_opt.phpt b/Zend/tests/list/list_from_array_literal_order_opt.phpt index 110032797ae6..5b1e597031c5 100644 --- a/Zend/tests/list/list_from_array_literal_order_opt.phpt +++ b/Zend/tests/list/list_from_array_literal_order_opt.phpt @@ -10,7 +10,7 @@ opcache.optimization_level=-1 ---EXPECT-- +--EXPECTF-- -- values before targets value 1 value 2 @@ -53,6 +53,8 @@ boom int(0) -- a variable that is a reference to a typed property construct coerced + +Deprecated: Implicit conversion from D to string for reference held by property S::$s of type string is deprecated in %s on line %d destruct coerced set y assigned diff --git a/Zend/tests/magic_methods/gh14969.phpt b/Zend/tests/magic_methods/gh14969.phpt index a69a739f0e37..81d5dea86f47 100644 --- a/Zend/tests/magic_methods/gh14969.phpt +++ b/Zend/tests/magic_methods/gh14969.phpt @@ -35,11 +35,14 @@ var_dump($d); ?> --EXPECTF-- +Deprecated: Implicit conversion from C to string for property D::$prop of type string is deprecated in %s on line %d Exception: C::__toString object(D)#%d (0) { ["prop"]=> uninitialized(string) } + +Deprecated: Implicit conversion from C to string for property D::$prop of type string is deprecated in %s on line %d Exception: C::__toString object(D)#2 (1) { ["prop"]=> diff --git a/Zend/tests/type_declarations/typed_properties_051.phpt b/Zend/tests/type_declarations/typed_properties_051.phpt index 47103a4efac5..bff2c1b91b9b 100644 --- a/Zend/tests/type_declarations/typed_properties_051.phpt +++ b/Zend/tests/type_declarations/typed_properties_051.phpt @@ -22,6 +22,7 @@ try { echo $e::class, ': ', $e->getMessage(), "\n"; } ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from B to string for property A::$a of type string is deprecated in %s on line %d string(4) "okok" TypeError: Cannot assign C to property A::$a of type string diff --git a/Zend/tests/type_declarations/typed_properties_089.phpt b/Zend/tests/type_declarations/typed_properties_089.phpt index 92de4b64aa24..aad8cabac60d 100644 --- a/Zend/tests/type_declarations/typed_properties_089.phpt +++ b/Zend/tests/type_declarations/typed_properties_089.phpt @@ -28,10 +28,13 @@ var_dump($a); ?> --EXPECTF-- +Deprecated: Implicit conversion from B to string for property A::$prop of type string is deprecated in %s on line %d object(A)#1 (1) { ["prop"]=> string(4) "test" } + +Deprecated: Implicit conversion from B to string for property A::$prop of type string is deprecated in %s on line %d object(A)#%d (1) { ["prop"]=> &string(4) "test" diff --git a/Zend/tests/type_declarations/typed_properties_107.phpt b/Zend/tests/type_declarations/typed_properties_107.phpt index 776ad4fc9f62..81ede2d7c99e 100644 --- a/Zend/tests/type_declarations/typed_properties_107.phpt +++ b/Zend/tests/type_declarations/typed_properties_107.phpt @@ -22,5 +22,6 @@ Test1::$ref = new Test3; var_dump(Test1::$ref); ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from Test3 to string for reference held by property Test2::$str of type string is deprecated in %s on line %d string(6) "foobar" diff --git a/Zend/tests/type_declarations/typed_properties_object_to_string_deprecation.phpt b/Zend/tests/type_declarations/typed_properties_object_to_string_deprecation.phpt new file mode 100644 index 000000000000..d2db4726b894 --- /dev/null +++ b/Zend/tests/type_declarations/typed_properties_object_to_string_deprecation.phpt @@ -0,0 +1,88 @@ +--TEST-- +Implicit conversion of objects to string for typed properties is deprecated +--FILE-- +s = new Str; +var_dump($t->s); + +$t->u = new Str; +var_dump($t->u); + +Test::$static = new Str; +var_dump(Test::$static); + +$ref = &$t->s; +$ref = new Str; +var_dump($t->s); +unset($ref); + +try { + $t->s = new stdClass; +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} + +class Gadget { + public string $s; + + public function __unserialize(array $data): void { + $this->s = $data['s']; + } +} + +var_dump(unserialize('O:6:"Gadget":1:{s:1:"s";O:3:"Str":0:{}}')); + +set_error_handler(function ($errno, $errstr) { + throw new ErrorException($errstr, 0, $errno); +}); + +$t->s = 'unchanged'; +try { + $t->s = new Str; +} catch (ErrorException $e) { + echo $e->getMessage(), "\n"; +} +var_dump($t->s); + +?> +--EXPECTF-- +Deprecated: Implicit conversion from Str to string for property Test::$s of type string is deprecated in %s on line %d +__toString() +string(3) "str" + +Deprecated: Implicit conversion from Str to string for property Test::$u of type string|int is deprecated in %s on line %d +__toString() +string(3) "str" + +Deprecated: Implicit conversion from Str to string for property Test::$static of type ?string is deprecated in %s on line %d +__toString() +string(3) "str" + +Deprecated: Implicit conversion from Str to string for reference held by property Test::$s of type string is deprecated in %s on line %d +__toString() +string(3) "str" +Cannot assign stdClass to property Test::$s of type string + +Deprecated: Implicit conversion from Str to string for property Gadget::$s of type string is deprecated in %s on line %d +__toString() +object(Gadget)#%d (1) { + ["s"]=> + string(3) "str" +} +Implicit conversion from Str to string for property Test::$s of type string is deprecated +string(9) "unchanged" diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index b166bf7dc75c..c7998ca85622 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -879,6 +879,28 @@ static zend_never_inline ZEND_COLD void zend_verify_property_type_error(const ze zend_string_release(type_str); } +/* Returns false when the error handler threw an exception. */ +static zend_never_inline ZEND_COLD bool zend_object_to_string_property_deprecated(const zend_property_info *info, const zval *property, bool is_ref) +{ + const zend_object *obj = Z_OBJ_P(property); + + /* Objects that cannot be converted only get the TypeError */ + if (!obj->ce->__tostring && obj->handlers->cast_object == zend_std_cast_object_tostring) { + return true; + } + + zend_string *type_str = zend_type_to_string(info->type); + zend_error(E_DEPRECATED, "Implicit conversion from %s to string for %sproperty %pS::$%s of type %pS is deprecated", + zend_zval_value_name(property), + is_ref ? "reference held by " : "", + info->ce->name, + zend_get_unmangled_property_name(info->name), + type_str); + zend_string_release(type_str); + + return !EG(exception); +} + static zend_never_inline ZEND_COLD void zend_magic_get_property_type_inconsistency_error(const zend_property_info *info, const zval *property) { /* we _may_ land here in case reading already errored and runtime cache thus has not been updated (i.e. it contains a valid but unrelated info) */ @@ -1066,6 +1088,10 @@ static zend_always_inline bool i_zend_check_property_type(const zend_property_in uint32_t type_mask = ZEND_TYPE_FULL_MASK(info->type); ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC|MAY_BE_NEVER|MAY_BE_VOID))); + if (UNEXPECTED(Z_TYPE_P(property) == IS_OBJECT) && !strict && (type_mask & MAY_BE_STRING) + && !zend_object_to_string_property_deprecated(info, property, false)) { + return 0; + } return zend_verify_scalar_type_hint(type_mask, property, strict, false); } @@ -4025,6 +4051,10 @@ ZEND_API bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference *ref, if (result < 0) { if (!first_prop) { first_prop = prop; + if (UNEXPECTED(Z_TYPE_P(zv) == IS_OBJECT) && (ZEND_TYPE_FULL_MASK(prop->type) & MAY_BE_STRING) + && !zend_object_to_string_property_deprecated(prop, zv, true)) { + return 0; + } ZVAL_COPY(&coerced_value, zv); if (!zend_verify_weak_scalar_type_hint( ZEND_TYPE_FULL_MASK(prop->type), &coerced_value)) { diff --git a/ext/dom/tests/DOMDocument_version_write.phpt b/ext/dom/tests/DOMDocument_version_write.phpt index ca6f88f8ac51..85c7c88cecbf 100644 --- a/ext/dom/tests/DOMDocument_version_write.phpt +++ b/ext/dom/tests/DOMDocument_version_write.phpt @@ -24,10 +24,12 @@ try { var_dump($dom->version); echo $dom->saveXML(); ?> ---EXPECT-- +--EXPECTF-- string(3) "1.0" string(6) "foobar" + +Deprecated: Implicit conversion from MyThrowingStringable to string for property DOMDocument::$version of type ?string is deprecated in %s on line %d Exception: An exception was thrown string(6) "foobar" diff --git a/ext/dom/tests/DOMElement_className.phpt b/ext/dom/tests/DOMElement_className.phpt index 184726e1f4ed..a7a4d630eb3e 100644 --- a/ext/dom/tests/DOMElement_className.phpt +++ b/ext/dom/tests/DOMElement_className.phpt @@ -34,13 +34,15 @@ var_dump($dom->documentElement->className); echo $dom->saveXML(); ?> ---EXPECT-- +--EXPECTF-- string(0) "" string(15) "hello & world<>" string(0) "" string(2) "é" string(0) "" string(5) "12345" + +Deprecated: Implicit conversion from MyStringable to string for property DOMElement::$className of type string is deprecated in %s on line %d Exception: foo string(5) "12345" diff --git a/ext/dom/tests/DOMElement_id.phpt b/ext/dom/tests/DOMElement_id.phpt index b810ba0fdb78..39a885684a73 100644 --- a/ext/dom/tests/DOMElement_id.phpt +++ b/ext/dom/tests/DOMElement_id.phpt @@ -37,13 +37,15 @@ echo $dom->saveXML(); var_dump($dom->getElementById("12345") === $div); ?> ---EXPECT-- +--EXPECTF-- string(0) "" string(15) "hello & world<>" string(0) "" string(2) "é" string(0) "" string(5) "12345" + +Deprecated: Implicit conversion from MyStringable to string for property DOMElement::$id of type string is deprecated in %s on line %d Exception: foo string(5) "12345" diff --git a/ext/dom/tests/DOMProcessingInstruction.phpt b/ext/dom/tests/DOMProcessingInstruction.phpt index 2602c0f92b3b..9bef260b971d 100644 --- a/ext/dom/tests/DOMProcessingInstruction.phpt +++ b/ext/dom/tests/DOMProcessingInstruction.phpt @@ -67,13 +67,15 @@ try { } ?> ---EXPECT-- +--EXPECTF-- --- Test construction --- DOMException: Invalid Character Error --- Test fields --- string(4) "test" string(0) "" string(2) "ok" + +Deprecated: Implicit conversion from FailingStringable to string for property DOMProcessingInstruction::$data of type string is deprecated in %s on line %d Exception: failed in __toString string(2) "ok" string(5) "12345" diff --git a/ext/dom/tests/toString_exceptions.phpt b/ext/dom/tests/toString_exceptions.phpt index 592ca0fc0bf9..e036905b0930 100644 --- a/ext/dom/tests/toString_exceptions.phpt +++ b/ext/dom/tests/toString_exceptions.phpt @@ -43,16 +43,35 @@ try { $pi->data = $badStr; } catch (Exception $e) { echo "Exception\n"; } echo $doc->saveXML(); ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from BadStr to string for property DOMDocument::$encoding of type ?string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMDocument::$version of type ?string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMDocument::$documentURI of type ?string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMAttr::$value of type string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMNode::$nodeValue of type ?string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMNode::$nodeValue of type ?string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMNode::$textContent of type string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMCharacterData::$data of type string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMNode::$prefix of type string is deprecated in %s on line %d Exception + +Deprecated: Implicit conversion from BadStr to string for property DOMProcessingInstruction::$data of type string is deprecated in %s on line %d Exception Text diff --git a/ext/opcache/tests/jit/assign_obj_op_002.phpt b/ext/opcache/tests/jit/assign_obj_op_002.phpt index a61678e97a97..ae5844ff05cf 100644 --- a/ext/opcache/tests/jit/assign_obj_op_002.phpt +++ b/ext/opcache/tests/jit/assign_obj_op_002.phpt @@ -23,7 +23,8 @@ $prop = &$a->prop; $a->prop = new B; var_dump($a); ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from B to string for property A::$prop of type string is deprecated in %s on line %d object(A)#1 (1) { ["prop"]=> &string(4) "test" diff --git a/ext/opcache/tests/jit/fetch_obj_008.phpt b/ext/opcache/tests/jit/fetch_obj_008.phpt index 69d3753a2ed3..b2ad104400c8 100644 --- a/ext/opcache/tests/jit/fetch_obj_008.phpt +++ b/ext/opcache/tests/jit/fetch_obj_008.phpt @@ -24,6 +24,8 @@ $a->prop = new B; ?> DONE --EXPECTF-- +Deprecated: Implicit conversion from B to string for property A::$prop of type string is deprecated in %s on line %d + Warning: Undefined variable $e in %sfetch_obj_008.php on line 9 Warning: Attempt to read property "prop" on string in %sfetch_obj_008.php on line 10 diff --git a/ext/reflection/tests/bug79683.phpt b/ext/reflection/tests/bug79683.phpt index 4eeafb6bb930..2d463e7a715e 100644 --- a/ext/reflection/tests/bug79683.phpt +++ b/ext/reflection/tests/bug79683.phpt @@ -27,7 +27,8 @@ $property->setValue($b, new A()); var_dump($b); ?> ---EXPECT-- +--EXPECTF-- +Deprecated: Implicit conversion from A to string for property B::$prop2 of type string is deprecated in %s on line %d object(B)#1 (1) { ["prop2":"B":private]=> string(3) "123"