0

Is it possible to initialize an objects private or protected members in php with an associative array.

for example:

    class TestClass
{
    public $_name;
    public $_age;


    public function __construct(array $params)
    {
        ??????
    }
}


$testClass = new TestClass(
    array(
        'name'  => 'Bob',
        'age' => '29',
    )
);

i was wondering whether there is an elegant solution - perhaps by implementing one the spl interfaces or otherwise?

5
  • the obvious solution is just $this->_name = $params['name']; in the constructor. What does your object do? Would the solution need to be anything more complex than that? Commented May 28, 2012 at 19:17
  • @bob-the-destroyer - the example provided is a simplified use case. In reality there will be a lot of properties to set - all of which are optional. Commented May 28, 2012 at 19:20
  • something just looping over $this or $params would solve? You mentioned SPL - were you thinking of having the purpose of the object behave as an array or something? Commented May 28, 2012 at 19:37
  • yes, i thought there may be a solution like that using spl Commented May 28, 2012 at 19:42
  • Just do it, it works. How to do that depends on your needs. Commented May 28, 2012 at 19:51

2 Answers 2

3
foreach ($params as $key=>$value)
{
 $key = '_'.$key;
 $this->$key=$value;
}

See the code online for working sample here

Sign up to request clarification or add additional context in comments.

1 Comment

That's what the line $key = '_' . $key is for. It changes 'age' to '_age' and 'name' to '_name'.
2

You mentioned SPL. But without knowing the exact requirements for the purpose of your object, the below is about the only information I can give...

You could have your object extend the SPL built-in class ArrayIterator. Then, without concern for handling it in the constructor (already handled in the parent ArrayIterator class), you could import an array into your object simply like so:

class testClass extends ArrayIterator
{

 /* child '__construct' method not required */

 /* rest of your code here */

}

$t = new testClass(array('name' => 'asdf', 'age' => 99));

Keep in mind that with default ArrayIterator behavior, you cannot later access any of the passed array values as you would with a normal object property. You must access them as you would an array:

echo $t['name']; // 'asdf'
echo $t->name; // NULL property unknown error

And, internally, the passed array is stored within your object as a single private storage parameter. In your case, you already have all your object properties pre-defined and prepended with an underscore, so you would probably have to manually loop over $this or $params anyway in your constructor to set any real object properties.

You could of course redefine all your child object's ArrayIterator inherited methods to handle your special property naming case on get or set, but this would seem redundant and unproductive as opposed to just looping over $params/setting $this anyway in your constructor.

    public function __construct(array $params)
    {
        foreach ($params as $key => $val) {
            if (property_exists($this, "_$key")) {
                $this->{"_$key"} = $val;
            }
        }
    }

So, just looping over $params/setting $this within your constructor is probably the best, most simple solution there is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.