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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教