Skip to content
Draft
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
4 changes: 3 additions & 1 deletion Zend/tests/exceptions/exception_009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion Zend/tests/gh10169.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion Zend/tests/list/list_from_array_literal_order.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ List assignment from an array literal: evaluation order and lifetime of the valu
<?php
require __DIR__ . '/list_from_array_literal_order.inc';
?>
--EXPECT--
--EXPECTF--
-- values before targets
value 1
value 2
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Zend/tests/list/list_from_array_literal_order_opt.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ opcache.optimization_level=-1
<?php
require __DIR__ . '/list_from_array_literal_order.inc';
?>
--EXPECT--
--EXPECTF--
-- values before targets
value 1
value 2
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/magic_methods/gh14969.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"]=>
Expand Down
3 changes: 2 additions & 1 deletion Zend/tests/type_declarations/typed_properties_051.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions Zend/tests/type_declarations/typed_properties_089.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion Zend/tests/type_declarations/typed_properties_107.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--TEST--
Implicit conversion of objects to string for typed properties is deprecated
--FILE--
<?php

class Str {
public function __toString(): string {
echo "__toString()\n";
return 'str';
}
}

class Test {
public string $s = '';
public int|string $u = 0;
public static ?string $static = null;
}

$t = new Test;
$t->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"
30 changes: 30 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/tests/DOMDocument_version_write.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ try {
var_dump($dom->version);
echo $dom->saveXML();
?>
--EXPECT--
--EXPECTF--
string(3) "1.0"
string(6) "foobar"
<?xml version="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"
<?xml version="foobar"?>
4 changes: 3 additions & 1 deletion ext/dom/tests/DOMElement_className.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
<?xml version="1.0"?>
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/tests/DOMElement_id.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
<?xml version="1.0"?>
Expand Down
4 changes: 3 additions & 1 deletion ext/dom/tests/DOMProcessingInstruction.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion ext/dom/tests/toString_exceptions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?xml version="1.0"?>
<root xmlns:ns="foo"><node attr="foo"/><node>Text</node><ns:node/><?pi foobar?></root>
3 changes: 2 additions & 1 deletion ext/opcache/tests/jit/assign_obj_op_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions ext/opcache/tests/jit/fetch_obj_008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ext/reflection/tests/bug79683.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading