3ehrang
3ehrang

Reputation: 629

what id differences between $this->{$spec} and $this->$spec?

also these two line:

$subForm = $this->{$spec}
$subForm = $spec;

public function prepareSubForm($spec)

{
    if (is_string($spec)) {
        $subForm = $this->{$spec};
    } elseif ($spec instanceof Zend_Form_SubForm) {
        $subForm = $spec;
    } else {
        throw new Exception('Invalid argument passed to ' .
                __FUNCTION__ . '()');
    }
    $this->setSubFormDecorators($subForm)
    ->addSubmitButton($subForm)
    ->addSubFormActions($subForm);
    return $subForm;
}

Upvotes: 2

Views: 67

Answers (1)

raina77ow
raina77ow

Reputation: 106395

It's said in the Variable Variables part of the documentation:

Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts

In this code there's no syntax-related reason to use $this->{$spec} instead of $this->$spec.

But:

  • the first form may be more readable to some teams (= enforced by the code conventions)

  • perhaps in the past there was an expression here (like $this->{'_' . $spec}), for example. And if you try to use an expression as 'variable property name', you need to use curve braces to delimit it.

As for difference between $spec and $this->$spec, it's more clear. This method can work with two types of its single argument:

  • if $spec is of String type, it's viewed as the name of property; this property is what will be processed (decorated) later;

  • if $spec is Zend_Form_SubForm object, this object will be decorated instead.

Upvotes: 1

Related Questions