# [PATCH] Make var_export() output "(object)array(..." instead of "stdClass::__set_state(..." for stdClass

- Thread: https://externals.io/message/98521
- Original archive: https://news-web.php.net/php.internals/98521
- Started by Andrea Faulds on 2017-03-14T18:57:34+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #98521 — Andrea Faulds — 2017-03-14T18:57:34+00:00

URL: https://externals.io/message/98521#98521

Hi everyone,

Since stdClass has no __set_state method, `var_export()` produces unusable 
output if given an object of that class. I wrote a patch that would make 
 `var_export()` produce a cast to object instead, which could be evaluated 
to get back a stdClass:

https://github.com/php/php-src/pull/2420

Any thoughts/comments?

If you're wondering about whether a __set_state method should be added 
to stdClass, I posted some thoughts in the pull request discussion already.

Thanks!
-- 
Andrea Faulds
https://ajf.me/

---

## #98522 — Fleshgrinder — 2017-03-14T19:26:42+00:00

URL: https://externals.io/message/98521#98522 · In reply to: #98521

> Hi everyone,
> 
> Since stdClass has no __set_state method, `var_export()` produces unusable
> output if given an object of that class. I wrote a patch that would make
> `var_export()` produce a cast to object instead, which could be evaluated
> to get back a stdClass:
> 
> https://github.com/php/php-src/pull/2420
> 
> Any thoughts/comments?
> 
> If you're wondering about whether a __set_state method should be added
> to stdClass, I posted some thoughts in the pull request discussion already.
> 
> Thanks!

I actually already used __set_state occasionally for userland classes.
Adding __set_state to stdClass seems to be more consistent with the rest
of PHP, rather than special casing it in one method. I cannot see any
issues with this to be honest.

-- 
Richard "Fleshgrinder" Fussenegger

---

## #98523 — Marc Bennewitz — 2017-03-14T20:01:04+00:00

URL: https://externals.io/message/98521#98523 · In reply to: #98522

Am 14.03.2017 um 20:26 schrieb Fleshgrinder:
> 
> > […]
>
> I actually already used __set_state occasionally for userland classes.
> Adding __set_state to stdClass seems to be more consistent with the rest
> of PHP, rather than special casing it in one method. I cannot see any
> issues with this to be honest.
>

Personally I would like to have a more reasonable way in general. No 
special case and no magic method.

So my proposal then would be to try to add a class cast operator (needs 
an own RFC) and later to with this approach on var_export.

Example:

class Foo {}

$std = new stdClass;
$foo = new Foo;
$arr = ['arr' => 'arr'];

var_dump((object)$std);   // no change
var_dump((array)$std);    // no change
var_dump((Foo)$std);      // object of class Foo
var_dump((object)$foo);   // no change
var_dump((stdClass)$foo); // object of class stdClass
var_dump((array)$foo);    // no change
var_dump((array)$arr);    // no change
var_dump((object)$arr);   // no change
var_dump((stdClass)$arr); // object of class stdClass
var_dump((Foo)$arr);      // object of class Foo

Thoughts?

Marc

---

## #98524 — Derick Rethans — 2017-03-14T22:20:06+00:00

URL: https://externals.io/message/98521#98524 · In reply to: #98521

> Hi everyone,
> 
> Since stdClass has no __set_state method, `var_export()` produces unusable
> output if given an object of that class. I wrote a patch that would make
> `var_export()` produce a cast to object instead, which could be evaluated to get
> back a stdClass:
> 
> https://github.com/php/php-src/pull/2420
> 
> Any thoughts/comments?

As original author of var_export, I approve of this — including not 
adding __set_state().

cheers,
Derick

---

## #98525 — Nikita Popov — 2017-03-14T22:59:35+00:00

URL: https://externals.io/message/98521#98525 · In reply to: #98524

> 
>
> > […]
> to get
> > […]
>
> As original author of var_export, I approve of this — including not
> adding __set_state().
>
> cheers,
> Derick
>

I'm also +1 on this, and also prefer not having __set_state(). An object
cast is both more idiomatic and more performant.

Additionally I feel that adding methods to stdClass will muddy the waters
-- for example, this means that extending stdClass is no longer entirely
unreasonable, as you might want to do it to reuse the __set_state()
implementation. Not something I want to see happening.

Nikita

---

## #98529 — Stanislav Malyshev — 2017-03-15T00:19:57+00:00

URL: https://externals.io/message/98521#98529 · In reply to: #98525

Hi!

> Additionally I feel that adding methods to stdClass will muddy the waters
> -- for example, this means that extending stdClass is no longer entirely
> unreasonable, as you might want to do it to reuse the __set_state()
> implementation. Not something I want to see happening.

I'm afraid that ship has sailed long ago:
https://github.com/search?q=%22extends+stdclass%22&type=Code&utf8=%E2%9C%93

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #98538 — Andrea Faulds — 2017-03-15T15:51:56+00:00

URL: https://externals.io/message/98521#98538 · In reply to: #98523

Hi Marc,

Marc Bennewitz wrote:
>
> Personally I would like to have a more reasonable way in general. No
> special case and no magic method.
>
> So my proposal then would be to try to add a class cast operator (needs
> an own RFC) and later to with this approach on var_export.

I've wondered if this would make sense before myself. But I think any 
such mechanism would need explicit support from the class being casted 
to, if the resulting internal object state is to make sense. stdClass 
wouldn't have that support.

Thanks.

-- 
Andrea Faulds
https://ajf.me/

---

## #102587 — Christoph M. Becker — 2018-07-04T21:33:02+00:00

URL: https://externals.io/message/98521#102587 · In reply to: #98521

> Since stdClass has no __set_state method, `var_export()` produces unusable
> output if given an object of that class. I wrote a patch that would make
> `var_export()` produce a cast to object instead, which could be evaluated
> to get back a stdClass:
> 
> https://github.com/php/php-src/pull/2420
> 
> Any thoughts/comments?
> 
> If you're wondering about whether a __set_state method should be added
> to stdClass, I posted some thoughts in the pull request discussion already.

FTR: If nobody objects, I'll merge this PR into master on 2018-07-12, so
that it goes into PHP 7.3.

See <https://github.com/php/php-src/pull/2420#issuecomment-402561913>.

-- 
Christoph M. Becker

---

## #102634 — Andreas Hennings — 2018-07-07T11:13:56+00:00

URL: https://externals.io/message/98521#102634 · In reply to: #102587

I do not disagree, just want to make an observation.

If multiple properties or array keys reference the same instance of
\stdClass, there will be multiple instances with identical values after a
round-trip with `var_export()` + eval().
This is not necessarily a problem, just something to be aware of.



> 
>
> > […]
> already.
>
> FTR: If nobody objects, I'll merge this PR into master on 2018-07-12, so
> that it goes into PHP 7.3.
>
> See <https://github.com/php/php-src/pull/2420#issuecomment-402561913>.
>
> --
> Christoph M. Becker

---

## #102787 — Andrea Faulds — 2018-07-12T11:46:26+00:00

URL: https://externals.io/message/98521#102787 · In reply to: #102587

Christoph M. Becker wrote:
> 
>
> > […]
>
> FTR: If nobody objects, I'll merge this PR into master on 2018-07-12, so
> that it goes into PHP 7.3.
>
> See <https://github.com/php/php-src/pull/2420#issuecomment-402561913>.
>

And so it was done: 
https://github.com/php/php-src/commit/e4e9cd835550990a6b8df7c61d59b6cc0da9b5b2

Thanks, Cristoph! :)

-- 
Andrea Faulds
https://ajf.me/
