I've not used PHP for a couple years—my focus has primarily been on C, C++, Objective-C, Ruby & ECMAScript. I'm having a night of keeping my mind fresh by experimenting in some other languages.
I came across an example on PHP.net of casting arrays to an object to enable accessing it's values by key as if they were instance variable:
$obj = (object)array('foo' => 'bar', 'property' => 'value');
As an exercise, I'd like to create a function that takes the same arguments as array
and returns the "array" already casted to an object. Something like:
function o_array(\* variable amount of key=>value args like array()*\)
{
return (object)array.call(self, arguments);
}
I don't know how to construct a function that takes a variable amount of key=>value arguments in PHP. Thanks in advance for the advice.
UPDATE
I hope the following changes in the future since I see PHP's main strength as being its flexibility and general craziness (also its weakness). Named parameters are not supported:
PHP language meeting regarding named params among other items.
array_merge()
? - php.net/array_merge – Tigger Feb 17 '13 at 9:21array
function. Bummer. I already now how to convert an array to an object, or cast an array to an object. Definitely not attempting to merge arrays. Not sure where that came from. – james_womack Feb 17 '13 at 9:24