access a property via string with array in php?

Posted by sprugman on Stack Overflow See other posts from Stack Overflow or by sprugman
Published on 2010-05-11T23:01:16Z Indexed on 2010/05/11 23:04 UTC
Read the original article Hit count: 110

Filed under:
|
|

(This is in drupal, but I don't really think that matters.)

I have a big list of properties that I need to map between two objects, and in one, the value that I need to map is buried inside an array. I'm hoping to avoid hard-coding the property names in the code.

If I have a class like this:

class Product {
    public $colors, $sizes;
}

I can access the properties like this:

$props = array('colors', 'sizes');
foreach ($props as $p) {
    $this->$p = $other_object->$p;
}

As far as I can tell, if each of the properties on the left are an array, I can't do this:

foreach ($props as $p) {
    $this->$p[0]['value'] = $other_object->$p;
}

Is that correct, or am I missing some clever way around this?

© Stack Overflow or respective owner

Related posts about php

Related posts about oop