Curly Braces Notation in PHP -
i reading source of opencart , ran such expression below. explain me:
$quote = $this->{'model_shipping_' . $result['code']}->getquote($shipping_address);
in statement, there weird code part is
$this->{'model_shipping_' . $result['code']}
has {}
, wonder is? looks object me not sure.
curly braces used denote string or variable interpolation in php. allows create 'variable functions', can allow call function without explicitly knowing is.
using this, can create property on object array:
$property_name = 'foo'; $object->{$property_name} = 'bar'; // same $object->foo = 'bar';
or can call 1 of set of methods, if have 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 used in strings more identify interpolation, if want to:
$hello = 'hello'; $result = "{$hello} world";
of course these simplifications. purpose of example code run 1 of number of functions depending on value of $result['code']
.
Comments
Post a Comment