Gumbo is right, here is a working example:
<?php
class Test
{
public $arr = array();
public $obj = null;
}
$a = new Test();
$a->arr[]->foo = 1234;
$a->arr[]->bar = 'test';
var_dump( $a->arr );
// even more weird on null objects
$a->obj->foobar = 'obj was null!';
var_dump( $a->obj );
returns:
array(2) {
[0]=>
object(stdClass)#2 (1) {
["foo"]=>
int(1234)
}
[1]=>
object(stdClass)#3 (1) {
["bar"]=>
string(4) "test"
}
}
object(stdClass)#4 (1) {
["foobar"]=>
string(13) "obj was null!"
}
edit: Okay, I found something related in the php manual about this:
If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. (source)
So using the ->
syntax converts the thing into an object. In the example above $obj
is null, so a new, empty instance is created, and the foobar
member is set.
When looking at the array example, arr[]
first creates a new (empty) array element, which is then converted into an empty object because of the ->
syntax and the member variable is set.