Re: [PATCH] Make var_export() output "(object)array(..." instead of "stdClass::__set_state(..." for stdClass
| From: | Marc Bennewitz | Date: | Tue, 14 Mar 2017 20:01:04 +0000 |
| Subject: | Re: [PATCH] Make var_export() output "(object)array(..." instead of "stdClass::__set_state(..." for stdClass | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98523@lists.php.net to get a copy of this message | ||
Am 14.03.2017 um 20:26 schrieb Fleshgrinder:
On 3/14/2017 7:57 PM, Andrea Faulds 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. Example: class Foo {} $std = new stdClass; $foo = new Foo; $arr = ['arr' => 'arr']; var_dump((object)$std); // no changeHi 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.
var_dump((array)$std); // no change var_dump((Foo)$std); // object of class Foovar_dump((object)$foo); // no change var_dump((stdClass)$foo); // object of class stdClass
var_dump((array)$foo); // no change var_dump((array)$arr); // no changevar_dump((object)$arr); // no change var_dump((stdClass)$arr); // object of class stdClass
var_dump((Foo)$arr); // object of class FooThoughts? Marc