Curly Braces Notation in PHP

前端 未结 3 1811
南笙
南笙 2020-12-01 11:10

I was reading source of OpenCart and I ran into such expression below. Could someone explain it to me:

$quote = $this->{\'model_shipping_\' . $result[\'co         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 11:22

    The name of the property is computed during runtime from two strings

    Say, $result['code'] is 'abc', the accessed property will be

    $this->model_shipping_abc
    

    This is also helpful, if you have weird characters in your property or method names.

    Otherwise there would be no way to distinguish between the following:

    class A {
      public $f = 'f';
      public $func = 'uiae';
    }
    
    $a = new A();
    echo $a->f . 'unc'; // "func"
    echo $a->{'f' . 'unc'}; // "uiae"
    

提交回复
热议问题