Christian Mayer's Weblog

PHP: Opposite of __set_state() like __get_state()?

[This post was originally posted as a Stackoverflow question.]

Is there an opposite PHP function for __set_state() like __get_state()? And I don’t mean __sleep() for serialization. I want a simple function which is called after var_export() is called on an object but before var_export() gets the data so I can choose on each object which data will be exported. I know there is a way to implement this with __get() and debug_backtrace() to modify the data only when var_export() is called on an object. But is there a simpler way?

Edit, 2014-06-11 07:17: There is no way to implement this with __get() and debug_backtrace() to modify the data only when var_export() is called on an object because __get() is not called on var_export().

Update, 2014-06-11 07:17: Solution: https://gist.github.com/TheFox/49ff6903da287c30e72f

Of course with an own implementation you can do everything. With is there a simpler way? I mean if there is a built-in PHP function or something like that so you don’t have to do it yourself. This solution is not easy because you must extend all your objects from Exportable. And this also only works when your variables are public. In this example I choosed to export only name and bar but not foo. A built-in PHP function (like __set_state() is) would be more nicer.

More Resources

Posted on .
Categories: Programming
Tags: PHP, Exportable, Export, set, get, state, Function, var, Variable

Categories | RSS Feed | Usage | Imprint
Copyright © 2006 by