我是公司的新实习生,当我看到这个符号时,我正在查看某人的代码:
if($o_eventNode->{$calOption}[0]['value'] == 1)
Run Code Online (Sandbox Code Playgroud)
我不理解带有花括号的部分.还有其他方法可以输入吗?这种语法的优点是什么?
使用该语法,您可以动态调用类的方法和变量,而无需知道它们各自的名称(另请参阅文档中的变量变量名称).
/ edit:用更复杂的例子更新答案,希望这更容易理解.修复了原始代码,这个应该实际工作.
例:
<?php
class Router {
// respond
//
// just a simple method that checks wether
// we got a method for $route and if so calls
// it while forwarding $options to it
public function respond ($route, $options) {
// check if a method exists for this route
if (method_exists($this, 'route_'.$route) {
// call the method, without knowing which
// route is currently requested
print $this->{'route_'.$route}($options);
} else {
print '404, page not found :(';
}
}
// route_index
//
// a demo method for the "index" route
// expecting an array of options.
// options['name'] is required
public function route_index ($options) {
return 'Welcome home, '.$options['name'].'!';
}
}
// now create and call the router
$router = new Router();
$router->respond('foo');
// -> 404, because $router->route_foo() does not exist
$router->respond('index', array('name'=>'Klaus'));
// -> 'Welcome home Klaus!'
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |