You write in your question:
I need a simple function called after calling var_export (), but before var_export () gets the data
It looks like you want to accept the var_export() function and then call the adapter instead of var_export() . Then, inside the adapter, you get the data before it is (really) var_export()ed :
function my_var_export($expression, $return = NULL) { $before = $expression; return var_export($before, $return); } $a = 'hello'; my_var_export($a);
With this adapter, you can technically install what you are looking for, but it is also a prerequisite.
So, we are concerned about objects, not strings. And for these objects, the __get_state() method should be called. Since we still know what this object should be (it must be exported), we create an interface:
interface Exportable { public function __get_state(); }
So how can this be implemented? One idea is to clone the real object and then modify it so that var_export does not encounter any problems with it. This cloning will save us from manipulating a specific object just for exporting it. But this is just a convention, an object that cannot be cloned can implement this __get_state() method, it might be a little harder to write an implementation.
And on the other hand, using the interface, the adapter my_var_export() can be more intelligent in how to treat $before when passing it to var_export() :
function my_var_export($expression, $return = null) { $before = $expression instanceof Exportable ? $expression->__get_state() : $expression; return var_export($before, $return); }
A new case has been introduced when exporting $expression requires special processing. This works as before for the expression $a = 'hello'; .
So now, to give it an edge, we need a specific type that is exportable, for example I use Foo here. For testing purposes, I give it a private property that is set only when the implementation of __get_state() was called in the accepted var_export() operation:
class Foo implements Exportable { private $name = null; private $__get_state_called; public function __construct($name) { $this->name = (string)$name; } public function __get_state() { $before = clone $this;
Working example than:
$b = new Foo('hello'); my_var_export($b);
Which gives the result as desired:
Foo::__set_state(array( 'name' => 'hello', '__get_state_called' => true, ))
And that your "magic" function __get_state() , which is called before var_export() , receives data, but after calling ( my_ ) var_export() .
Adapt the var_export function to add the necessary functions. Use the interface for objects that need special handling.
Full example ( run it on the Internet ):
<?php interface Exportable { public function __get_state(); } function my_var_export($expression, $return = null) { $before = $expression instanceof Exportable ? $expression->__get_state() : $expression; return var_export($before, $return); } class Foo implements Exportable { private $name = null; private $__get_state_called; public function __construct($name) { $this->name = (string)$name; } public function __get_state() { $before = clone $this;