dqyz48470 2016-01-25 20:18
浏览 43
已采纳

奇怪的php方法调用

I'm facing a strange way to call a method on a object.

$controller->{ $action }();

But if i remove the curly braces, the call will work anyway. Someone knows what those curly braces mean?

The current context

<?php
  function call($controller, $action) {
    // require the file that matches the controller name
    require_once('controllers/' . $controller . '_controller.php');

    // create a new instance of the needed controller
    switch($controller) {
      case 'pages':
        $controller = new PagesController();
      break;
    }

    // call the action
    $controller->{ $action }();
  }

  // just a list of the controllers we have and their actions
  // we consider those "allowed" values
  $controllers = array('pages' => ['home', 'error']);

  // check that the requested controller and action are both allowed
  // if someone tries to access something else he will be redirected to the error action of the pages controller
  if (array_key_exists($controller, $controllers)) {
    if (in_array($action, $controllers[$controller])) {
      call($controller, $action);
    } else {
      call('pages', 'error');
    }
  } else {
    call('pages', 'error');
  }
?>

UPDATE

$controller and $action are variables inherited from a index.php file which requires this one. So as inherited variables they are fully accessible.

Here's index.php

//  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';
  • 写回答

1条回答 默认 最新

  • dongshubang7816 2016-01-25 20:27
    关注

    As Dagon linked you to, the method name in that example is a variable variable.

    The braces are not required if you are just using a variable name on its own, however if you wanted to concatenate a string into the variable name then you'd need the braces, e.g.:

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

    $action in your example represents a method name, e.g. Run, so you could run $controller->customRun().

    In your context, it's an abstracted way of calling a controller's action based on providing $controller and $action.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog