I am confused about constructor. I need to pass an array to a class. I have at the moment 2 data to pass to the array.
Now from this array, I need to define $this->m_id and $this->community_type so that I can use these variables through out the class. Below is my example.
$arr = array('id'=>$u_id, 'community_type' => $community_type);
$rate = new Registration($arr);
class Registration{
protected $m_id;
protected $community_type;
public function __construct(array $arr = array())
{
foreach ($arr as $key => $value) {
$this->$key = $value;
}
}
}
I am looking to set
$this->m_id = $m_id;
$this->community_type = $community_type;
I tried using a for loop but I don't know something went wrong.
Can anybody help me
$rate->idand$rate->community_typecommunity_typebeing set asprotected, I missed that sorry. Wait for a full answer.