Using __set_state()
The __set_state()
magic method is triggered (not really) for classes exported by the var_export()
 function. The method accepts a single array type parameter and returns an object, as per the following synopsis:
static object __set_state(array $properties)
The var_export()
function outputs or returns a parsable string representation of a given variable. It is somewhat similar to the var_dump()
function, except that the returned representation is a valid PHP
<?php class User { public $name = 'John'; public $age = 34; private $salary = 4200.00; protected $identifier = 'ABC'; } $user = new User(); var_export($user); // outputs string "User::__set_state..." var_export($user, true); // returns string "User::__set_state..."
This results in the following output:
User::__set_state(array( 'name' => 'John', 'age' => 34, 'salary' => 4200.0, 'identifier' => 'ABC', )) string(113) "User::__set_state(array( 'name' => 'John', 'age' => 34, 'salary' =...