简体   繁体   English

php类函数作为$ this->的参数

[英]php class function as parameter with $this->

I have a webservice class with a number of private functions. 我有一个带有许多私有功能的webservice类。 I have a main function which decides which function to call according to the first parameter of the web service call. 我有一个主要函数,该函数根据Web服务调用的第一个参数来决定要调用哪个函数。

$actions = [
        "CHECK_LOGIN"     => "checkLogin" 
    ,   "CHECK_FB"        => "checkFB"  
    ,   "GET_DETAILS"     => "getDetails" 
    ,   "SAVE_DETAILS"    => "saveDetails" 
    ,   "CHANGE_PWD"      => "saveDetails" 
    ,   "SEND_ECODE"      => "sendEmailCode"
]

So, its easy to find which function to call according to the parameter passed: 因此,根据传递的参数很容易找到要调用的函数:

if (!array_key_exists($action, $actions))
  $rep = ["status" => STATUS_ERROR, "errMsg" => "Action '$action' unknown"];
else {
   $method = $actions[$action];
   $rep = $method($req);
}

My problem is that I can't call "$method" what ever it is (eg "checkFB"), I need to call $this->method (ie "$this->checkFB") How do I put the $this-> in front of the function name found in the $actions array?? 我的问题是我无法调用“ $ method”(例如“ checkFB”),我需要调用$ this-> method(即“ $ this-> checkFB”)如何放置$ this ->在$ actions数组中找到的函数名称前面?

You can do it like this: 您可以这样做:

if (!array_key_exists($action, $actions))
  $rep = ["status" => STATUS_ERROR, "errMsg" => "Action '$action' unknown"];
else {
   $method = $actions[$action];
   $rep = $this->$method($req);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM