Array notation is fine, but it can look a bit clunky when you’re working with complex structures. This is a fairly simple example, but I’m sure we’ve all dealt with worse:
$clientChanges['deletes'][$val['fkClient']] = $val['Total'];
Casting the array to an object allows us to use object notation (->) and makes the code more readable:
$val = (object) $val;
$clientChanges = (object) $clientChanges;
$clientChanges->deletes[$val->fkClient] = $val->Total;
You can even get away with using array functions on objects, as long as they’re just simple collections of properties:
$o3 = (object) array_merge( (array) $o1, (array) $o2 );
Of course, member functions won’t make it through this kind of mangling, but bizarrely, private variables do:
class O
{
private $a = 4;
var $b = 5;
var $c = 6;
}
$o = new O();
$o = ( object ) array_reverse( ( array )$o );
var_dump( $o );
/**
* outputs:
*
* object(stdClass)#2 (3) {
* ["c"]=> int(6)
* ["b"]=> int(5)
* ["a:private"]=> int(4)
* }
*/