2
public function __construct($template = '', array $data = array())
{
    if ($template !== '') {
        $this->setTemplate($template);
    }
    if (!empty($data)) {
        foreach ($data as $name => $value) {
            $this->$name = $value;
        }
    }
}

Got this from the devshed composite view tutorial (http://www.devshed.com/c/a/PHP/PHP-Composite-View-Design-Pattern-Introducing-the-Key-Concepts/1/). Anyway, I'm a bit confuse on, $this->$name = $value; statement.

I usually use $this for class's properties and/or when invoking class's methods within the said class. Plus the statement have two $'s. Which is weird! So is $this->$name = $value referring to the $name defined in the foreach loop? If so can someone explain this usage or logic behind this?

Thank you in advance.

4 Answers 4

2

The $name in

$this->$name = $value

Sets the variable defined as $name to $value

E.g. if $name = 'user', then this is the equivalent to

$this->user = value;

This type of syntax is often used (as demonstrated above) in foreach loops to set object values.

Note: Each time $this->variable_name is called, if 'variable_name' is not already defined as a property of the object, the magic __set function is called, with 'variable_name' passed as the argument.

See http://php.net/manual/en/language.oop5.overloading.php

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

3 Comments

Here right, but only true, if name does not exists as real property. In this case the magic properties are not called.
@KingCrunch - Yes, this is true, will edit my response to make it a bit clearer
I think I got it. After messing around with the AbstractView class. It seems that $this->$name is referring to a dynamically created $name by __set. So if say, a class property isn't explicitly define in the class, a user can basically do: $foo = new AbstractView; $foo->bar = 'stuff';, so that the __set function is called and create a bar class property to the abstractview class. And $this->$name = $value statement is referring to the $name variable, a place holder, that is used in the other magic methods defined through out the AbstractView class, hence the extra $. Thank you so much!
1

I didn't go to the link you posted but reading the code.

I will give you an example:

// If $data has the following values
array(
    'firstname' => 'my first name',
    'surname'  => 'my surname'
)

The code will have 'firstname' and 'surname' as a public property of the class. The values for $this->firstname will be 'my first name'. The value of $this->surname will be 'my surname'.

Comments

0

$name contain the name of property nothing else

instead of just mentioning the property statically it is pointing to the variable which contain the name of property

$name='someproperty';

$this->$name;

Comments

0

Let as assume that the current value of $name is "foo". then $this->name will return exactly the same as $this->foo.

1 Comment

You mean $this->$name. And not return, but rather refer.

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.