10

Suppose I have:

class A{
 public $one;
 public $two;
}

and an array with values:

array('one' => 234, 'two' => 2)

is there a way to have an instance of A filled with the right values from the array automatically?

4
  • Define automatically. Does the instance of A already exists or must it be created? If yes, with or without calling the constructor of that instance? Commented Mar 6, 2012 at 15:39
  • 1
    Duplicate of [PHP: How to turn an array into a StdClass object]? stackoverflow.com/questions/1885160/… Commented Mar 6, 2012 at 15:42
  • @Rob2211: I searched as well, but A ain't StdClass. Commented Mar 6, 2012 at 15:47
  • Sorry, I assumed StdClass was similar to Java's Object, it's not. Commented Mar 6, 2012 at 15:53

4 Answers 4

17

You need to write yourself a function for that. PHP has get_object_varsDocs but no set counterpart:

function set_object_vars($object, array $vars) {
    $has = get_object_vars($object);
    foreach ($has as $name => $oldValue) {
        $object->$name = isset($vars[$name]) ? $vars[$name] : NULL;
    }
}

Usage:

$a = new A();
$vars = array('one' => 234, 'two' => 2);
set_object_vars($a, $vars);
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! What would I need to change to get the same effect for another 2 dimension steps? $object['x']['y']['z'];
NB This DOES NOT REVEAL PROTECTED and PRIVATE variables if used as a normal function. As 95% of all variables I use are protected for obvious reasons this does not work. (Wasted 20 mins on this.) Tried putting it in the class but then lots of array_key_exists needs 2 param warnings and no result so gave up. Might be usable with @menacingly suggestion but too clever for me so off to fudge it.
2

If you want to allow for bulk-setting of attributes, you can also store them as a property. It allows you to encapsulate within the class a little better.

class A{
  protected $attributes = array();
  function setAttributes($attributes){
    $this->attributes = $attributes;
  }

  public function __get($key){
    return $this->attributes[$key];
  }

}

Comments

2

@hakre version is quite good, but dangerous (suppose an id or password is in thoses props).

I would change the default behavior to that:

function set_object_vars($object, array $vars) {
    $has = get_object_vars($object);
    foreach ($has as $name => $oldValue) {        
        array_key_exists($name, $vars) ?  $object->$name =$vars[$name] : NULL;
    }
}

here, the previous properties that are not in the $vars array are not affected.
and if you want to set a prop to null on purpose, you can.

3 Comments

array_key_exists() function has two params - key & search.
yes, right, I suppose I wrote it on the fly without testing. It then shall be: array_key_exists($name, $vars)
only works if object is initialize with defaults. with no constructor or default values it just slips thru
-3

Yes there is.

You could use a pass thru method.

For example:

class A {
    public $one, $tow;
    function __construct($values) {
        $this->one = $values['one'] ?: null;
        $this->two = $values['two'] ?: null;
    }
}

$a = new A(array('one' => 234, 'two' => 2));

1 Comment

@Rob2211 true, but it is the best that I can think of.

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.