The implicit assumption here is that objects have significant, supposedly public, properties that provide values ββfor the calling code. This is by no means predetermined β encapsulation is a key aspect of OOP, so the primary access to an object is through its methods.
The "correct" mechanism for initializing the state of an object is its constructor, and not a series of property assignments. What arguments the constructor takes are consistent with the class definition.
Now the constructor may have a long series of named parameters, so you could write $foo = new WhatEver(1, "hello", false, null) , but if you want them to act as parameters, then it could take one hash - in PHP terms, an array as its argument.
So, to answer the question, yes, if your constructor has the form function __construct(Array $options) , and then iterates over or checks for $options . But it depends on the constructor what to do with these options; for example, passing [ 'use_safe_options' => true ] can cause the entire set of private variables to be set to documented βsafeβ values.
Starting with PHP 5.4 (which introduced [ ... ] as an alternative to array( ... ) ), it takes only a few character strokes than the Perl version:
$foo = new WhatEver( ['bar' => 'baz'] );
IMSoP
source share