How to have an object like Javascript... stdClass or (object) array();?

after using javascript quite a bit, I am used to

var obj = {};
obj.foo = 1;
obj.bar = “hello”;

etc…

and so in PHP, is the standard way to doing that by

$obj = new stdClass();
$obj->foo = 1;
$obj->bar = “hello”;

? I think $obj = (object) array() can also be used. but probably stdClass() is a more standard way? is there also like an object literal like { } as in javascript? This is kind of like defining an arbitrary object and we can tag on any property… in a less traditional object oriented way where we always define a class and then use $obj = new Animal() i think. In Javascript, associative array and object are actually the same thing… but in PHP, they are different huh? thanks.

There is no such thing as an associative array in JavaScript. Array inherits from Object, so you can add properties to an Array as if it were an Object. If you add “elements” with keys to a JavaScript array and then query its length property, you will see that those elements do not count towards its length the same way that numerically indexed elements do. Why? Because they are properties of the Array object and not elements of the array.