Php 这个语法是什么意思$节点->;{option}
我是一家公司的新实习生,我正在查看某人的代码,当我看到以下符号时:Php 这个语法是什么意思$节点->;{option},php,syntax,Php,Syntax,我是一家公司的新实习生,我正在查看某人的代码,当我看到以下符号时: if($o_eventNode->{$calOption}[0]['value'] == 1) 我不明白有花括号的部分。有没有其他方法可以打出来?这种语法的优点是什么?使用这种语法,您可以动态调用类的方法和变量,而不必知道它们各自的名称(另请参见文档) /编辑:用一个更复杂的例子更新了答案,我希望这更容易理解。修正了原来的代码,这个应该可以正常工作 例如: <?php class Router {
if($o_eventNode->{$calOption}[0]['value'] == 1)
我不明白有花括号的部分。有没有其他方法可以打出来?这种语法的优点是什么?使用这种语法,您可以动态调用类的方法和变量,而不必知道它们各自的名称(另请参见文档) /编辑:用一个更复杂的例子更新了答案,我希望这更容易理解。修正了原来的代码,这个应该可以正常工作
例如:
<?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!'
?>
使用这种语法,您可以动态调用类的方法和变量,而不必知道它们各自的名称(另请参见文档)
/编辑:用一个更复杂的例子更新了答案,我希望这更容易理解。修正了原来的代码,这个应该可以正常工作
例如:
<?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!'
?>
变量$calOption
的内容将用作$o_eventNode
中类成员的名称。花括号在那里,用于清楚地标记变量的结尾,因此很明显,这里的意思不是$calOption[0]['value']
请参阅:有关将变量与数组一起使用时此歧义问题的解释。变量$calOption
的内容将用作$o_eventNode
中类成员的名称。花括号在那里,用于清楚地标记变量的结尾,因此很明显,这里的意思不是$calOption[0]['value']
请参阅:有关在数组中使用变量时出现的歧义问题的解释。@BorisGuery我不这么认为,这些问题涉及完全不同的主题。@BorisGuery我不这么认为,这些问题涉及完全不同的主题。