PHP object literal In PHP, I can specify array literals quite easily: array( array("name" => "John", "hobby" => "hiking"), array("name" => "Jane", "hobby" => "dancing"), ... ) But what if I want array of objects? How can I specify object literal in PHP? I.e. in javascript it would be: [ {name: "John", hobby: "hiking"}, {name: "Jane", hobby: "dancing"} ] there is no object literal in PHP however you can do this by simply type casting the arrays to objects: $testArray = array( (object)array("name" => "John", "hobby" => "hiking"), (object)array("name" => "Jane", "hobby" => "dancing") ); echo "Person 1 Name: ".$testArray[0]->name; echo "Person 2 Hobby: ".$testArray[1]->hobby; Do note that objects are always passed by reference, arrays are not. So changing an attribute of an object changes it everywhere the object has been assigned.
This is not the case for arrays; they remain independent. As of PHP 5.4 you can also use the short array syntax: $json = [ (object) ['name' => 'John', 'hobby' => 'hiking'], (object) ['name' => 'Jane', 'hobby' => 'dancing'], ]; Another way would be to use the __set_state() magic method: $object = stdClass::__set_state (array ( 'height' => -10924, 'color' => 'purple', 'happy' => false, 'video-yt' => 'AgcnU74Ld8Q' )); As others have noted, there is no object literal in PHP, but you can "fake" it by casting an array to an object. With PHP 5.4, this is even more concise, because arrays can be declared with square brackets. For example: $obj = (object)[ "foo" => "bar", "bar" => "foo", ]; This would give you an object with "foo" and "bar" properties. However, I don't think this really provides much advantage over using associative arrays. It's just a syntax difference. Consider embracing the uniqueness and "flavor" of all the languages you use. In JavaScript, object literals are all over the place. In PHP, associative arrays are functionally the same as JavaScript object literals, are easy to create, and are well understood by other PHP programmers. I think you're better off embracing this "flavor" than trying to make it feel like JavaScript's object literal syntax. In PHP, you have ro create an instance before you can use a class. Of course you can put the instances into an array later. If you're wanting to define modules in the object literal pattern "like JavaScript", you could do something like this: $object = (object) [ 'config' => (object) [ 'username' => 'Rob Bennet', 'email' => 'rob@madebyhoundstooth.com' ], 'hello' => function() use(&$object) { return "Hello " . $object->config->username . ". "; }, 'emailDisplay' => function() use(&$object) { return "Your email address is " . $object->config->email; }, 'init' => function($options) use(&$object) { $object->config = $options; $doUsername = $object->hello; $doEmail = $object->emailDisplay; return $doUsername() . $doEmail(); } ]; $sayMyInfo = $object->init; echo $sayMyInfo((object) [ 'username' => 'Logan', 'email' => 'wolverine@xmen.com' ]); In this type of modular scenario, I usually opt for the facade pattern, I just like writing my calls thusly: Module::action()->item; or Post::get()->title; Neither of these patterns make it easy (or even possible sometimes) for testing. But this is just proof of concept. Technically, "no" there is no Object Literal in PHP, but if you're used to JavaScript syntax (which I am more so than PHP), you can fake it and do this. As you can see, it's a lot messier in PHP than in JavaScript. PHP.net says... This about creating instances of objects. A full page of objective wizardry. EDIT: At the time of posting, PHP.net was useful, but since, has become more useful (that's progress for ya!). See user added notes for tips and tricks, where you'll find: $a = (object) array('property1' => 1, 'property2' => 'b'); posted 4 years ago. And yes, that note has possitive reputation ;-) look: http://php.net/manual/en/language.oop5.basic.php#example-170

look: https://en.wikipedia.org/wiki/Facade_pattern

look: http://php.net/manual/en/language.types.array.php

look: http://blog.thoughtlabs.com/blog/2008/02/02/phps-mystical-__set_state-method