Does PHP feature short hand syntax for objects?

In javascript you can easily create objects and Arrays like so:

var aObject = { foo:'bla', bar:2 };
var anArray = ['foo', 'bar', 2];

Are similar things possible in PHP?
I know that you can easily create an array using the array function, that hardly is more work then the javascript syntax, but is there a similar syntax for creating objects? Or should I just use associative arrays?

$anArray = array('foo', 'bar', 2);
$anObjectLikeAssociativeArray = array('foo'=>'bla',

So to summarize:
Does PHP have javascript like object creation or should I just use associative arrays?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

For simple objects, you can use the associative array syntax and casting to get an object:

$obj = (object)array('foo' => 'bar');
echo $obj->foo; // yields "bar"

But looking at that you can easily see how useless it is (you would just leave it as an associative array if your structure was that simple).

Solution 2

There was a proposal to implement this array syntax. But it was declined.

Update    The shortened syntax for arrays has been rediscussed, accepted, and is now on the way be released with PHP 5.4.

But there still is no shorthand for objects. You will probably need to explicitly cast to object:

$obj = (object) ['foo'=>'bla', 'bar'=>2];

Solution 3

As of PHP 5.4, you can do this:

$array = ['foo'=>'bla', 'bar'=>2];

It’s not much shorter, but you’ll appreciate it if you need to use a lot of hard coded nested arrays (which isn’t altogether uncommon).

If you want an object, you would still need to cast each array:

$object = (object) ['foo'=>'bla', 'bar'=>2];

Solution 4

According to the new PHP syntaxes,

You can use

$array = [1,2,3];

And for associative arrays

$array = ['name'=>'Sanket','age'=>22];

For objects you can typecast the array to object

$object = (object)['name'=>'Sanket','age'=>22];

Solution 5

There is no object shorthand in PHP, but you can use Javascript’s exact syntax, provided you use the json_encode and json_decode functions.

Solution 6

The method provided by crescentfresh works very well but I had a problem appending more properties to the object. to get around this problem I implemented the spl ArrayObject.

class ObjectParameter extends ArrayObject  {
     public function  __set($name,  $value) {
        $this->$name = $value;

    public function  __get($name) {
      return $this[$name];

//creating a new Array object
$objParam = new ObjectParameter;
//adding properties
$objParam->strName = "foo";
//print name
//add another property
$objParam->strUser = "bar";

There is a lot you can do with this approach to make it easy for you to create objects even from arrays, hope this helps .

Solution 7

Like the json_decode idea, wrote this:

function a($json) {
 return json_decode($json, true); //turn true to false to use objets instead of associative arrays

$cat = 'meow';

$array = a('{"value1":"Tester", 


Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply