php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #67918 var_export does not export a parsable string representation of stdClass objects
Submitted: 2014-08-27 17:46 UTC Modified: 2015-09-13 20:43 UTC
Votes:3
Avg. Score:2.3 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: php-bugs-2014-08 at dotancohen dot com Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.5.16 OS: Linux melancholy 3.13.0-34-gener
Private report: No CVE-ID: None
 [2014-08-27 17:46 UTC] php-bugs-2014-08 at dotancohen dot com
Description:
------------
Though var_export() is designed to "outputs or return a parsable string representation of a variable", the output of stdClass objects is not "a parsable string". Witness:

php > $foo = new stdClass();
php > var_export($foo);
stdClass::__set_state(array(
))
php > $bar = stdClass::__set_state(array());
PHP Fatal error:  Call to undefined method stdClass::__set_state() in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. {main}() php shell code:0

Relevant quote from the fine manual:

> var_export() gets structured information about the given variable. It
> is similar to var_dump() with one exception: the returned representation
> is valid PHP code.

Note that the output presented above is not valid PHP code.

It seems that bug #48016 attacks the same issue from the other side (stdClass::__setState is not defined although var_export uses it), however that bug was closed prematurely, as the test case used to justify the close does not demonstrate that the issue is resolved.

Test script:
---------------
$ php -a
php > $foo = new stdClass();
php > var_export($foo);
stdClass::__set_state(array(
))
php > $bar = stdClass::__set_state(array());
PHP Fatal error:  Call to undefined method stdClass::__set_state() in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. {main}() php shell code:0



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-09 13:44 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2015-09-09 13:44 UTC] cmb@php.net
Indeed, bug #48016 has been closed erroneously, so I re-opened it.
Instead of marking this ticket as duplicate, I'm switching to
documentation problem, because the current behavior has to be
documented anyway.
 [2015-09-09 14:12 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=337793
Log: documented necessity that __set_state() must be implemented to re-eval var_export()ed code (fixes #67918)
 [2015-09-09 14:13 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2015-09-09 14:13 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2015-09-13 20:43 UTC] derick@php.net
This should really be fixed in the code though. stdClass should implement __set_state as per https://bugs.php.net/bug.php?id=48016
 [2019-08-06 23:45 UTC] roy-orbison at devo dot net dot au
It is sufficient to search and replace "stdClass::__set_state" with "(object)", sans quotes, to make previously exported data parsable again.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 09 08:01:34 2024 UTC