php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48016 stdClass::__setState is not defined although var_export() uses it
Submitted: 2009-04-19 12:51 UTC Modified: 2018-07-12 11:35 UTC
Votes:9
Avg. Score:4.0 ± 0.9
Reproduced:8 of 8 (100.0%)
Same Version:4 (50.0%)
Same OS:4 (50.0%)
From: c dot r1 at gmx dot de Assigned: ajf (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.3 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
13 - 11 = ?
Subscribe to this entry?

 
 [2009-04-19 12:51 UTC] c dot r1 at gmx dot de
Description:
------------
var_export() for an object which is just used for storing properties and is of stdClass produces invalid code as stdClass::__setState is not implemented, for no appearant reason. It would be nice to have an implementation of this in 5.3.0, and, if possible, a workaround for earlier versions of php.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-08 20:31 UTC] jani@php.net
-Package: Feature/Change Request +Package: Class/Object related -PHP Version: 5.2.9 +PHP Version: 5.3
 [2013-08-06 06:20 UTC] yohgaki@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: yohgaki
 [2013-08-06 06:20 UTC] yohgaki@php.net
It seems issue is fixed.

[yohgaki@dev PHP-5.4]$ ./php-bin -a
Interactive shell

php > $o = new stdClass;
php > var_export($o);
stdClass::__set_state(array(
))php > $o->foo = 123;
php > $o->bar = 456;
php > var_export($o);
stdClass::__set_state(array(
   'foo' => 123,
   'bar' => 456,
))php >
 [2015-09-09 13:42 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened
 [2015-09-09 13:42 UTC] cmb@php.net
@yohgaki: The issue is not that var_export() fails, but rather
that the exported representation can't be eval'd (i.e. imported
again), see <https://3v4l.org/XSPRH>. So either StdClass should
implement __set_state() or var_export() should export something
like the following for StdClass instances:

  (object) array(...)
 [2017-03-14 17:30 UTC] ajf@php.net
This pull request would fix this, if merged: https://github.com/php/php-src/pull/2420

Albeit not by adding __set_state, but by using an (object) cast instead.
 [2017-10-24 03:07 UTC] kalle@php.net
-Status: Re-Opened +Status: Assigned
 [2018-07-05 19:59 UTC] ajf@php.net
-Assigned To: yohgaki +Assigned To: ajf
 [2018-07-12 11:35 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2019-08-06 23:53 UTC] roy-orbison at devo dot net dot au
Because the fix does not make data exported the old way parsable (I assume for consistent behaviour), you can repair it by replacing "stdClass::__set_state" with "(object)", sans quotes.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 19:01:30 2024 UTC