21

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

$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address);

In the statement, there is a weird code part that is
$this->{'model_shipping_' . $result['code']}
which has {} and I wonder what that is? It looks an object to me but I am not really sure.

Czechnology
  • 14,832
  • 10
  • 62
  • 88
Tarik
  • 79,711
  • 83
  • 236
  • 349

3 Answers3

36

Curly braces are used to denote string or variable interpolation in PHP. It allows you to create 'variable functions', which can allow you to call a function without explicitly knowing what it actually is.

Using this, you can create a property on an object almost like you would an array:

$property_name = 'foo';
$object->{$property_name} = 'bar';
// same as $object->foo = 'bar';

Or you can call one of a set of methods, if you have some sort of REST API class:

$allowed_methods = ('get', 'post', 'put', 'delete');
$method = strtolower($_SERVER['REQUEST_METHOD']); // eg, 'POST'

if (in_array($method, $allowed_methods)) {
    return $this->{$method}();
    // return $this->post();
}

It's also used in strings to more easily identify interpolation, if you want to:

$hello = 'Hello';
$result = "{$hello} world";

Of course these are simplifications. The purpose of your example code is to run one of a number of functions depending on the value of $result['code'].

leemeichin
  • 3,339
  • 1
  • 24
  • 31
  • 1
    Thanks, I've found this explanation very comprehensible and broad. – Tarik Jan 30 '12 at 06:03
  • 1
    In your first example, simply writing `$object->$property_name = 'bar'` would have the exact same effect. I suppose the braces are just necessary when it's a non-trivial expression – Cruncher Aug 05 '17 at 12:05
10

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"
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Why was nobody saying anything? The code as it stood was not working at all (function invocation missing). Fixed now by using vars instead of functions. – knittl Jan 18 '13 at 19:33
4

Curly braces are used to explicitly specify the end of a variable name.

https://stackoverflow.com/a/1147942/680578

http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

Community
  • 1
  • 1
Kristian
  • 21,204
  • 19
  • 101
  • 176
  • 3
    Bare-link answers are not answers. If you feel that this question duplicates another, flag it for closure. Otherwise, you should elaborate on the links you cite. – BoltClock Jan 29 '12 at 19:29
  • I know the curly braces in strings but the curly braces are not wrapped up with quotation marks in my example. – Tarik Jan 29 '12 at 19:31