Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
奇怪的php方法调用_Php_Oop_Model View Controller_Methods_Function Call - Fatal编程技术网

奇怪的php方法调用

奇怪的php方法调用,php,oop,model-view-controller,methods,function-call,Php,Oop,Model View Controller,Methods,Function Call,我正面临一种对对象调用方法的奇怪方式 $controller->{ $action }(); 但是如果我去掉大括号,这个电话无论如何都能用。有人知道那些花括号是什么意思吗 当前环境 当Dagon将您链接到时,该示例中的方法名称为 如果只是单独使用变量名,则不需要大括号,但是如果要将字符串连接到变量名中,则需要大括号,例如: // These are the same: $controller->$action(); $controller->{$action}(); //

我正面临一种对对象调用方法的奇怪方式

$controller->{ $action }();
但是如果我去掉大括号,这个电话无论如何都能用。有人知道那些花括号是什么意思吗

当前环境
当Dagon将您链接到时,该示例中的方法名称为

如果只是单独使用变量名,则不需要大括号,但是如果要将字符串连接到变量名中,则需要大括号,例如:

// These are the same:
$controller->$action();
$controller->{$action}();

// This won't work:
$controller->custom$action();
// This will work:
$controller->{'custom' . $action}();
$action
在您的示例中表示方法名称,例如
Run
,因此您可以运行
$controller->customRun()


在您的上下文中,它是一种基于提供
$controller
$action

奇怪语法调用控制器操作的抽象方法。什么是上下文?可能是刚刚编辑的@darkomen的副本。@Dagon,我已经阅读了手册,但我看不出在这种上下文中这样做的逻辑原因……但是在这种上下文中使用这种类型的调用值得吗?当然-这是常见的做法,您只需要在调用之前验证操作是否存在it@Caius只是永远不要删除其中的
$controller->
部分,运行类似
{$action}()的代码可能非常危险。@cmorrissey您为什么要这样做?肯定会弄坏控制器的system@cmorrissey为什么我必须从{$action}();中删除$controller->?
//  set default controller and action
$controller =   'login';
$action     =   'index';

//  check if $_GET variables are set
if(isset($_GET['controller']) && $_GET['action'])
{
    //  if we have something set in here we override the default value
    $controller = $_GET['controller'];
    $controller = $_GET['action'];
}

//  now we require the router file who will read the $controller and $action vars.
require_once '../app/core/Router.php';
// These are the same:
$controller->$action();
$controller->{$action}();

// This won't work:
$controller->custom$action();
// This will work:
$controller->{'custom' . $action}();