Skip to content

Commit e4e9cd8

Browse files
hikari-no-yumecmb69
authored andcommitted
Export stdClass objects using (object) cast (fixes #48016)
Before this change, var_export()'s output for stdClass objects calls the non-existent stdClass::__set_state method, and is therefore useless. This commit makes var_export() output an (object) cast from an array instead, which when evaluated, will produce a stdClass object. Other classes see unchanged output.
1 parent 7d4e18b commit e4e9cd8

File tree

10 files changed

+62
-47
lines changed

10 files changed

+62
-47
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ PHP NEWS
9999
. Fixed bug #76505 (array_merge_recursive() is duplicating sub-array keys).
100100
(Laruence)
101101
. Fixed bug #71848 (getimagesize with $imageinfo returns false). (cmb)
102+
. Fixed bug #48016 (stdClass::__setState is not defined although var_export() uses it). (Andrea)
102103

103104
- Tokenizer:
104105
. Fixed bug #76538 (token_get_all with TOKEN_PARSE flag fails to recognise

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ PCRE:
398398
behavioral changes, and augments the existing regular expression syntax.
399399
See <https://wiki.php.net/rfc/pcre2-migration> for details.
400400

401+
Standard:
402+
. var_export() now exports stdClass objects as an array casted to an object
403+
(`(object) array( ... )`), rather than using the nonexistent method
404+
stdClass::__setState().
405+
401406
ZIP:
402407
. Bundled libzip has been dropped,
403408
system library is now required.

ext/intl/tests/dateformat_format.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,27 +397,27 @@ Date is: DateTime::__set_state(array(
397397
------------
398398
Formatted DateTime is : 20001230 05:04 PM
399399
------------
400-
Date is: stdClass::__set_state(array(
401-
))
400+
Date is: (object) array(
401+
)
402402
------------
403403
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
404404
------------
405-
Date is: stdClass::__set_state(array(
406-
))
405+
Date is: (object) array(
406+
)
407407
------------
408408
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
409409
------------
410-
Date is: stdClass::__set_state(array(
411-
))
410+
Date is: (object) array(
411+
)
412412
------------
413413
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
414414
------------
415-
Date is: stdClass::__set_state(array(
416-
))
415+
Date is: (object) array(
416+
)
417417
------------
418418
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
419419
------------
420-
Date is: stdClass::__set_state(array(
421-
))
420+
Date is: (object) array(
421+
)
422422
------------
423423
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'

ext/intl/tests/dateformat_format_variant3.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,27 +397,27 @@ Date is: DateTime::__set_state(array(
397397
------------
398398
Formatted DateTime is : 20001230 05:04 PM
399399
------------
400-
Date is: stdClass::__set_state(array(
401-
))
400+
Date is: (object) array(
401+
)
402402
------------
403403
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
404404
------------
405-
Date is: stdClass::__set_state(array(
406-
))
405+
Date is: (object) array(
406+
)
407407
------------
408408
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
409409
------------
410-
Date is: stdClass::__set_state(array(
411-
))
410+
Date is: (object) array(
411+
)
412412
------------
413413
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
414414
------------
415-
Date is: stdClass::__set_state(array(
416-
))
415+
Date is: (object) array(
416+
)
417417
------------
418418
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'
419419
------------
420-
Date is: stdClass::__set_state(array(
421-
))
420+
Date is: (object) array(
421+
)
422422
------------
423423
Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTimeInterface permitted): U_ILLEGAL_ARGUMENT_ERROR'

ext/standard/tests/array/var_export.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ $a = (object) array (1, 3, "foo" => "bar");
66
var_export($a);
77
?>
88
--EXPECT--
9-
stdClass::__set_state(array(
9+
(object) array(
1010
'0' => 1,
1111
'1' => 3,
1212
'foo' => 'bar',
13-
))
13+
)

ext/standard/tests/general_functions/var_export-locale.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -914,12 +914,12 @@ string(41) "array (
914914
*** Output for objects ***
915915

916916
Iteration 1
917-
stdClass::__set_state(array(
918-
))
919-
stdClass::__set_state(array(
920-
))
921-
string(31) "stdClass::__set_state(array(
922-
))"
917+
(object) array(
918+
)
919+
(object) array(
920+
)
921+
string(17) "(object) array(
922+
)"
923923

924924

925925
Iteration 2

ext/standard/tests/general_functions/var_export_basic6.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ foreach($valid_objects as $key => $obj) {
108108
*** Output for objects ***
109109

110110
-- Iteration: new stdclass --
111-
stdClass::__set_state(array(
112-
))
113-
stdClass::__set_state(array(
114-
))
115-
string(31) "stdClass::__set_state(array(
116-
))"
111+
(object) array(
112+
)
113+
(object) array(
114+
)
115+
string(17) "(object) array(
116+
)"
117117

118118

119119
-- Iteration: new foo --

ext/standard/tests/general_functions/var_export_basic8.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ var_export($b);
3030
===DONE===
3131
--EXPECT--
3232
-- Var export on a simple object --
33-
stdClass::__set_state(array(
33+
(object) array(
3434
'a' => 1,
3535
'b' =>
3636
array (
3737
'k' => 2,
3838
),
3939
'x' =>
40-
stdClass::__set_state(array(
40+
(object) array(
4141
'p' => '22',
42-
)),
43-
))
42+
),
43+
)
4444

4545
-- Var export on an simple array --
4646
array (
@@ -57,15 +57,15 @@ array (
5757
'one' => 'first',
5858
),
5959
'bar' =>
60-
stdClass::__set_state(array(
60+
(object) array(
6161
'a' => 1,
6262
'b' =>
6363
array (
6464
'k' => 2,
6565
),
6666
'x' =>
67-
stdClass::__set_state(array(
67+
(object) array(
6868
'p' => '22',
69-
)),
70-
)),
69+
),
70+
),
7171
)===DONE===

ext/standard/tests/general_functions/var_export_basic9.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Bug #55082: var_export() doesn't escape properties properly
66
$x->{'\'\\'} = 7;
77
echo var_export($x);
88
--EXPECT--
9-
stdClass::__set_state(array(
9+
(object) array(
1010
'\'\\' => 7,
11-
))
11+
)

ext/standard/var.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,13 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
527527
buffer_append_spaces(buf, level - 1);
528528
}
529529

530-
smart_str_append(buf, Z_OBJCE_P(struc)->name);
531-
smart_str_appendl(buf, "::__set_state(array(\n", 21);
530+
/* stdClass has no __set_state method, but can be casted to */
531+
if (Z_OBJCE_P(struc) == zend_standard_class_def) {
532+
smart_str_appendl(buf, "(object) array(\n", 16);
533+
} else {
534+
smart_str_append(buf, Z_OBJCE_P(struc)->name);
535+
smart_str_appendl(buf, "::__set_state(array(\n", 21);
536+
}
532537

533538
if (myht) {
534539
ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
@@ -539,7 +544,11 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
539544
if (level > 1) {
540545
buffer_append_spaces(buf, level - 1);
541546
}
542-
smart_str_appendl(buf, "))", 2);
547+
if (Z_OBJCE_P(struc) == zend_standard_class_def) {
548+
smart_str_appendc(buf, ')');
549+
} else {
550+
smart_str_appendl(buf, "))", 2);
551+
}
543552

544553
break;
545554
case IS_REFERENCE:

0 commit comments

Comments
 (0)