Reputation: 3516
I have this class,
class example {
public $a;
public $b;
public $c;
....
}
I have an array containing equal number of public variables as present in the example class (in this case 3):
$arr[0] = 'Red';
$arr[1] = 'Green';
$arr[2] = 'Blue';
I want to assign each value of this array to the public properties of the class one by one (Like $a
gets Red
, $b
gets Green
and so on). How do I assign these values from the array to the public properties of the class using a loop?
I was writing something like below, but it didn't work:
$class = new example();
$i = 0;
foreach ($class as $key => $value) {
$class->$$key = $arr[$i];
$i++;
}
EDIT:
Just to explain why I couldn't use any setter/getter method in example
class - actually example
class is created by unserializing a database object. I do not have any control on this class. I am working on a controller class which receives this example
class and an array. I need to figure out a way to assign the values from this array to the public properties of example
class.
Upvotes: 2
Views: 2913
Reputation: 14071
Well, as I mentioned before - the approach is wrong. There are two answers that tell you how to do it, but it's not the best way.
This is a slightly better way, and it's smarter - because you have less code, less work and you can add as many properties as you want without ever touching the class.
The code:
class MyClass {
protected $_data = array();
public function setData(array $data)
{
$this->_data = $data;
}
public function __set($key, $value)
{
$this->_data[$key] = $value;
}
public function __get($key)
{
return isset($this->_data[$key]) ? $this->data[$key] : null;
}
}
Usage scenario #1:
$class = new MyClass;
$class->red = 'My red value';
$class->blue = 'My blue value';
Usage scenario #2:
$class = new MyClass;
$class->setData( array('red' => 'Red', 'blue' => 'Blue', 'green' => 'Green') );
The example serves to steer you towards magic functions __get() and __set() which you can use to your advantage and code the class (with proper data and value checking) in such a way that it's usable without having to declare its properties. This reduces complexity. Or you can use one of the given answers if they fit your scenario in a satisfactory way.
Upvotes: 2
Reputation: 12836
Here you go:
<?php
class example {
public $a;
public $b;
public $c;
}
$arr[0] = 'Red';
$arr[1] = 'Green';
$arr[2] = 'Blue';
$class = new example();
$i = 0;
foreach ($class as $key => $value) {
$class->{$key} = $arr[$i];
$i++;
}
var_dump($class);
The basic idea is to use $class->{$key}
instead of $class->$$key
, because $$key
is a variable variable.
Upvotes: 1
Reputation: 64526
It doesn't work because of the double $$
when you assign the key:
$class->$$key = $arr[$i];
Change to a single $
$class->$key = $arr[$i];
When accessing object properties without a variable, no $
is needed, therefore if your key is stored in a variable, a single $
is needed.
That said, there is likely a better way to solve the problem than this. Seems like the X Y Problem.
Upvotes: 2