Although I honestly do not understand why you want to do this (associative arrays are essentially data-only objects), but if you insist:
Instead of casting each individual array at each level to an object, you can use the following trick / hack:
$object = json_decode( json_encode( array('some'=>array('multi'=>'Dimensional'), 'array'=>'that', 'you' => array('want' => 'to', 'turn' => 'into'), 'an' => 'object')));
This will convert all arrays to stdClass instances, which I believe is what you wanted.
Again, I have to say: PHP is not JavaScript, and objects are much more expensive (relatively speaking) in languages ββlike PHP, then they are in JS. I highly recommend you use files with arrays if you don't need an object.
Like objects, arrays can be entered with types: function foo (array $argument){}
If you really want to turn them into a concrete instance of a class, why not change the constructor to work with an array:
class My_Object extends stdClass { public function __construct(array $params = null) { if (!empty($params)) { foreach ($params as $name => $value) { $this->{$name} = $value;
And add all the methods you want to add to the lot
Elias van ootegem
source share