简体   繁体   中英

Is it possible to set a class property by name?

Simple question here... I was wondering if it's possible to access class propertys by it's name in any way.

Imagine this scenario:

$array = array(
    'foo' => 'bar',
    'hello' => 'world',
    'chuck' => 'norris'
);

And then have this class:

class MegaClass {
    public $foo, $hello, $chuck;
}

I was wondering if I could set MegaClass::foo to bar , MegaClass::hello to world and so on, automatically. So, given the array, and given the object, the object is filled. This could be really handy when retreiving data from a form with properties filled...

Well for one thing if you wanted to do MegaClass::foo they would have to be static.

And if they were static you could do:

foreach($array as $key=>$val){
    MegaClass::$key = $val;
}

And if they were not static (but were still public):

foreach($array as $key=>$val){
    $MegaClassObject->$key = $val;
}

For non-static members, then

$mc = new MegaClass();
foreach($array as $k=>$v) $mc->$k = $v;

should do. If that's what you ask for.

For static you can assign as

MegaClass::${$k} = $v;

but only if the static property is declared.

Something like this, as a minimum, would do what you are asking:

public function setProperties($array) {
  foreach($array as $key => $value) {
    $this->$key = $value;
  }
}

I would consider flushing it out a little though, perhaps by checking that the property exists and that it has not already been set. You would, of course, modify it to your particular requirements.

public function setProperties($array) {
  foreach($array as $key => $value) {
    if (property_exists(get_called_class(), $key)) {
      if ($this->$key === NULL) {
        $this->$key = $value;
      }
    }
  }
}

you can do it with a simple public function and foreach in your class.

class MegaClass {
  public function setarray(array $array){
    foreach($array as $key = > $value){
       $this->$key = $value;
    }
  }
}

$array = array(
    'foo' => 'bar',
    'hello' => 'world',
    'chuck' => 'norris'
);

$megaclass = new MegaClass();
$megaclass->setarray($array); // this wil set the array to the vars

echo $megaclass->foo; // this will output bar

foreach sets the $array to 2 other vars here that is the $key and $value. in the $key stands foo, hello and chuck and in the $value stands bar, world and norris.

the foreach loop loops every thing one by one and sets it to the right array.

php foreach menual

Not 100% sure I understood your question, but assuming your property is not static, you need an instance:

$m = new MegaClass();
echo $m->foo;

But having a bunch of public fields and setting them externally by looping through some array is probably not the best design. Why not just pass the array to a constructor of MegaClass, and let it initialize its fields internally? Alternatively, have a static factory method that creates a new instance from that array.

If I understand your question correctly....

$mc = new MegaClass()
foreach( $array as $key=>$value ) {
    $mc->$key = $value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM