0

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

3
  • outside of the class, $rate->id and $rate->community_type Commented Jul 21, 2017 at 3:47
  • @Scuzzy i didnot get it can you please explain Commented Jul 21, 2017 at 3:49
  • Actually, you're going to run into issues with that community_type being set as protected, I missed that sorry. Wait for a full answer. Commented Jul 21, 2017 at 3:52

2 Answers 2

1

You could try $array[your_array_key] as follow

public function __construct(array $arr = array())
{
    $this->m_id = $arr['id'];
    $this->community_type = $arr['community_type'];
}

Your existing code should work, if you are trying through loop, only problem I could notice is,

  protected $m_id;

You need to change this to

 protected $id;

Because, in your loop you are assuming your key is the member variable, which is actually not.

   foreach ($arr as $key => $value) {
        $this->$key = $value;
    }

In you are array, your first key is id where as member variable declared as m_id, which is not matching.

Sign up to request clarification or add additional context in comments.

2 Comments

i just include 2 items in the array but lets say i have many then what can i do
then you could loop it through. I will update the answer.
1

When run in the terminal, it shows that the object's properties are being created dynamically exactly as one would expect:

php > class Registration{
php {     protected $m_id;
php {     protected $community_type;
php { 
php {     public function __construct(array $arr = array())
php {     {
php {         foreach ($arr as $key => $value) {
php {             $this->$key = $value;
php {         }
php {     }
php { }
php > $u_id = 'u_id value';
php > $community_type = 'community type value';
php > 
php > $arr = array('id'=>$u_id, 'community_type' => $community_type);
php > $rate = new Registration($arr);
php > 
php > var_dump($rate);
object(Registration)#1 (3) {
  ["m_id":protected]=>
  NULL
  ["community_type":protected]=>
  string(20) "community type value"
  ["id"]=>
  string(10) "u_id value"
}

I think there were several confounding factors that may have tripped you up:

  1. Were the $u_id and $community_type variables assigned? They weren't in your code.
  2. There was some confusion about the variable names: $m_id vs $u_id, ['id'] vs $this->m_id
  3. Protected makes them harder access.

The var_dump shows that the keys of your array (['id'] and ['community_type'] were indeed assigned as properties of the object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.