php5: arrays, stdClass and best practice
php5.2+ is all about objects and a maturing OOP language. Up until now the reliable method to structure an associative list of items is by using an array
-
$arA = new array();
-
$arA['one'] = 1;
-
$arA['two'] = array('one','two');
-
$arA['three'] = 'three';
All well and good. Nothing earth shattering there.
With php5's changed object model and the passing of objects by reference, as well as the ability to type-hint function arguments - and in php6 function returns - the power of objects as data holders is more integral to best practices.
-
$stA = new stdClass;
-
$stA->one = 1;
-
$stA->two = array('one','two');
-
$stA->three = 'three';
-
-
//type-hint forcing
-
function passStd(stdClass $std) {
-
//do stuff...
-
}
Again, not-earth shattering, but neater (IMO) and better OOP practice. I'm sure there are many opinions on this as well as a much deeper usages - e.g with SPL.
I've not done any baseline testing for speed differences. Maybe someone with knowledge (and time!) has the info.
Test




