php - From Real stdClass Object to Mock -


i have soap webservice returns stdclass object different properties. want create mock object simulates stdclass returned webservice. dont want create mock object manually. dont want serialize, unserialize object because want maybe edit propertie values inside vcs.

so need turn this:

stdclass object (     [parama] => 1     [paramb] => 2     [paramc] => 3     [paramd] => array     (         [0] => stdclass object             (                 [paramd1] => 'blabla'                 [paramd2] => 'blabla' 

into this:

$object = new stdclass; $object -> parama = 1; $object -> paramb = 2; $object -> paramc = 3; $object -> paramd -> paramd1 = "blabla"; $object -> paramd -> paramd2 = "blabla"; 

how it?

a way build stdclass objects cast array object (outlined in type juggling):

$object = (object) array('prop' => 'value'); 

and shows, keys become property names , values values:

echo $object->prop; # value 

this works inside each other, having array of child objects:

$object = (object) array(     'prop'     => 'value',     'children' => array(         (object) array('prop' => 'value'),     ), ); 

which give along lines like:

echo $object->children[0]->prop; # value 

which looks you're looking for.

see convert array object php more examples , variations.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -