Skip to content
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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ PHP NEWS
. Fixed bug #76505 (array_merge_recursive() is duplicating sub-array keys).
(Laruence)
. Fixed bug #71848 (getimagesize with $imageinfo returns false). (cmb)
. Fixed bug #48016 (stdClass::__setState is not defined although var_export() uses it). (Andrea)

- Tokenizer:
. Fixed bug #76538 (token_get_all with TOKEN_PARSE flag fails to recognise
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ PCRE:
behavioral changes, and augments the existing regular expression syntax.
See <https://wiki.php.net/rfc/pcre2-migration> for details.

Standard:
. var_export() now exports stdClass objects as an array casted to an object
(`(object) array( ... )`), rather than using the nonexistent method
stdClass::__setState().

ZIP:
. Bundled libzip has been dropped,
system library is now required.
Expand Down
20 changes: 10 additions & 10 deletions ext/intl/tests/dateformat_format.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -397,27 +397,27 @@ Date is: DateTime::__set_state(array(
------------
Formatted DateTime is : 20001230 05:04 PM
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
20 changes: 10 additions & 10 deletions ext/intl/tests/dateformat_format_variant3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -397,27 +397,27 @@ Date is: DateTime::__set_state(array(
------------
Formatted DateTime is : 20001230 05:04 PM
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
------------
Date is: stdClass::__set_state(array(
))
Date is: (object) array(
)
------------
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
4 changes: 2 additions & 2 deletions ext/standard/tests/array/var_export.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $a = (object) array (1, 3, "foo" => "bar");
var_export($a);
?>
--EXPECT--
stdClass::__set_state(array(
(object) array(
'0' => 1,
'1' => 3,
'foo' => 'bar',
))
)
12 changes: 6 additions & 6 deletions ext/standard/tests/general_functions/var_export-locale.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -914,12 +914,12 @@ string(41) "array (
*** Output for objects ***

Iteration 1
stdClass::__set_state(array(
))
stdClass::__set_state(array(
))
string(31) "stdClass::__set_state(array(
))"
(object) array(
)
(object) array(
)
string(17) "(object) array(
)"


Iteration 2
Expand Down
12 changes: 6 additions & 6 deletions ext/standard/tests/general_functions/var_export_basic6.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ foreach($valid_objects as $key => $obj) {
*** Output for objects ***

-- Iteration: new stdclass --
stdClass::__set_state(array(
))
stdClass::__set_state(array(
))
string(31) "stdClass::__set_state(array(
))"
(object) array(
)
(object) array(
)
string(17) "(object) array(
)"


-- Iteration: new foo --
Expand Down
16 changes: 8 additions & 8 deletions ext/standard/tests/general_functions/var_export_basic8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ var_export($b);
===DONE===
--EXPECT--
-- Var export on a simple object --
stdClass::__set_state(array(
(object) array(
'a' => 1,
'b' =>
array (
'k' => 2,
),
'x' =>
stdClass::__set_state(array(
(object) array(
'p' => '22',
)),
))
),
)

-- Var export on an simple array --
array (
Expand All @@ -57,15 +57,15 @@ array (
'one' => 'first',
),
'bar' =>
stdClass::__set_state(array(
(object) array(
'a' => 1,
'b' =>
array (
'k' => 2,
),
'x' =>
stdClass::__set_state(array(
(object) array(
'p' => '22',
)),
)),
),
),
)===DONE===
4 changes: 2 additions & 2 deletions ext/standard/tests/general_functions/var_export_basic9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Bug #55082: var_export() doesn't escape properties properly
$x->{'\'\\'} = 7;
echo var_export($x);
--EXPECT--
stdClass::__set_state(array(
(object) array(
'\'\\' => 7,
))
)
15 changes: 12 additions & 3 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,13 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
buffer_append_spaces(buf, level - 1);
}

smart_str_append(buf, Z_OBJCE_P(struc)->name);
smart_str_appendl(buf, "::__set_state(array(\n", 21);
/* stdClass has no __set_state method, but can be casted to */
if (Z_OBJCE_P(struc) == zend_standard_class_def) {
smart_str_appendl(buf, "(object) array(\n", 16);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The hardest two things in computer science are, of course, off-by-one errors. This was briefly smart_str_appendl(buf, "(object) array(\n", 15); and I was confused why there was no newline…

Copy link
Member

Choose a reason for hiding this comment

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

sizeof(…)-1 to the rescue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to do one better and use ZEND_STRL, but I can't because smart_str_appendl is a macro. I could use smart_str_appends, but that it uses strlen annoys me at some level, even though any reasonable compiler™ will do the right thing…

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I was not aware of either! ZEND_STRL is … fascinating! Anyway, smart_str_appends might still be preferable here – after all, it's just about a potentially missing optimization, and I prefer clean code over any optimization, unless the latter proves to be worth the trouble. I'm fine with your current solution, though, since it doesn't make things worse.

} else {
smart_str_append(buf, Z_OBJCE_P(struc)->name);
smart_str_appendl(buf, "::__set_state(array(\n", 21);
}

if (myht) {
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
Expand All @@ -539,7 +544,11 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
if (level > 1) {
buffer_append_spaces(buf, level - 1);
}
smart_str_appendl(buf, "))", 2);
if (Z_OBJCE_P(struc) == zend_standard_class_def) {
smart_str_appendc(buf, ')');
} else {
smart_str_appendl(buf, "))", 2);
}

break;
case IS_REFERENCE:
Expand Down