S.A.N wrote on 12/02/2016 15:21:
It's not just a matter of taste, the object is always passed by
reference, an array is copied when you change, object literal syntax
like JSON, sorely lacking in PHP.
Objects are passed by *pointer*, because they're expected to have
methods that mutate their state in place. An object with arbitrary keys
is unlikely to have any such methods, so I still don't really see the
point: if you want to manipulate an array in place, put an & in your
function signature.
If you actually want an object that contains arbitrary data, just have a
single property called $data with the array in; that way, you can have
"real" (declared) properties alongside, e.g.
class CacheItem {
private $data = [];
private $last_updated;
public function __construct($data) {
$this->data = $data;
$this->last_updated = time();
}
}
You could even implement __get and __set so that the keys of $data were
exposed as though they were public properties.
stdClass has always seemed completely pointless to me, because it
doesn't provide any encapsulation, and making a "real" object (i.e. a
declared class, with declared properties) is so easy, and so much more
powerful.
That's why I call it a matter of taste: if I want a hash, I'll use an
array, which is already full-featured; if I want an object, I'll define
a class for it and use at least some OO principles.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php