簡體   English   中英

php類函數作為$ this->的參數

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

我有一個帶有許多私有功能的webservice類。 我有一個主要函數,該函數根據Web服務調用的第一個參數來決定要調用哪個函數。

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

因此,根據傳遞的參數很容易找到要調用的函數:

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

我的問題是我無法調用“ $ method”(例如“ checkFB”),我需要調用$ this-> method(即“ $ this-> checkFB”)如何放置$ this ->在$ actions數組中找到的函數名稱前面?

您可以這樣做:

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