doutui8842 2018-12-31 16:51
浏览 79
已采纳

细长的路由和控制器(这个__call函数如何从路由调用以及为什么$ this->请求使用$ request =}

guys please help me to understand these code

in picture 1 router is calling with info method

as you can see in AccountController there is already info() is present then why __call() this magic function is calling

and what are these parameters is $this->request ,$this->response

we can save all data like

$request = $args[0]; $response = $args[1]; $attributes = $args[2];

why $this-> syntex is use what is the meaning of this line $this->$name();

Router.php

<?php

$app->get('/account', '\App\Controller\AccountController:info');

?>

AccountController.php

<?php
/**
 * AccountController
 * @package Controllers
 */
namespace App\Controller;


final class AccountController extends \App\Core\Controller
{

    protected function info()
    {

        echo $this->client_id;

    }
}

Controller.php

 <?php

namespace App\Core;


 class Controller
{


  public function __call($name,$args) { //line 25
        //echo "Call method : ".$name;
        $this->request = $args[0];
        $this->response = $args[1];
        $this->attributes = $args[2];

        //print_r($this->attributes);
        $this->client_id = $this->request->getAttribute("client_id");



              $this->$name();

    }

}


?>
  • 写回答

2条回答 默认 最新

  • dongting7352 2018-12-31 17:56
    关注

    Router.php called your info() method on AccountController.php But your method is protected and info() method not accessible out of the class so __call() magic method has been called with $name and $args parameters.

    $name => value is method name. "info".
    $args => value array of response,request,attributes

    $this => It's a reference to the current object, it's most commonly used in the object-oriented code. What does the variable $this mean in PHP?

    request,response,attributes,client_id they are variables of the controller class and accessible on each method of controller class children. like $this->client_id in your AccountController class.

        $this->request = $args[0];
        $this->response = $args[1];
        $this->attributes = $args[2];
        $this->client_id = $this->request->getAttribute("client_id");
    

    $this->$name(); This dymnaic way for call the methods.

    PHP OOP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器