How to create an object literal in PHP
We all know that we can create object literals in JavaScript like this:
const person = { "name": "Saurabh", "profession": "Software Developer"};
But recently I needed to use a temporary object literal in my PHP code. A little bit of googling around revealed that there is no direct way to create an object literal in PHP.
We can of course use an associative array literal like this:
$person = array( "name" => "Saurabh", "profession" => "Software Developer" );
echo $person["name"] . "<br/>";echo $person["profession"];
/*SaurabhSoftware Developer*/
But in my particular use case, I needed to use an object and not an associative array.
Fortunately there are workarounds.
Type cast an associative array to an object
You can type cast an associative array into an object. The keys of the associative array become the properties of the object and and are assigned values respectively.
$person = (object)array( "name" => "Saurabh", "profession" => "Software Developer" );
echo $person->name . "<br/>";echo $person->profession;
/*SaurabhSoftware Developer*/
Instantiate an empty generic php object
You can also create an empty generic PHP object and then specify properties and values on it like this.
$person = new stdClass();$person->name = "Saurabh";$person->profession = "Software Developer";
echo $person->name . "<br/>";echo $person->profession;
/*SaurabhSoftware Developer*/