Doc #67918 [Com]: var_export does not export a parsable string representation of stdClass objects
| From: | roy-orbison at devo dot net dot au | Date: | Tue, 06 Aug 2019 23:45:32 +0000 |
| Subject: | Doc #67918 [Com]: var_export does not export a parsable string representation of stdClass objects | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16866@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67918&edit=1
ID: 67918
Comment by: roy-orbison at devo dot net dot au
Reported by: php-bugs-2014-08 at dotancohen dot com
Summary: var_export does not export a parsable string
representation of stdClass objects
Status: Closed
Type: Documentation Problem
Package: Class/Object related
Operating System: Linux melancholy 3.13.0-34-gener
PHP Version: 5.5.16
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
It is sufficient to search and replace "stdClass::__set_state" with "(object)",
sans quotes, to make previously exported data parsable again.
Previous Comments:
------------------------------------------------------------------------
[2015-09-13 20:43:36] 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
------------------------------------------------------------------------
[2015-09-09 14:13:26] 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-09 14:12:21] 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 13:44:20] 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.
------------------------------------------------------------------------
[2014-08-27 17:46:47] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67918&edit=1