Reputation: 9282
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...
Upvotes: 0
Views: 208
Reputation: 146302
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;
}
Upvotes: 2
Reputation: 609
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.
Upvotes: 1
Reputation: 77995
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.
Upvotes: 0
Reputation: 2298
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;
}
}
}
}
Upvotes: 1
Reputation: 143071
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.
Upvotes: 1
Reputation: 1255
If I understand your question correctly....
$mc = new MegaClass()
foreach( $array as $key=>$value ) {
$mc->$key = $value;
}
Upvotes: 0