dtnpzghys01643322 2015-06-24 17:39
浏览 48
已采纳

用于REST HTTP谓词的PHP __call方法

I am writing a REST wrapper class which looks like:

class RestApi
{
      public function __call($method, $resource, $data = []){
           return $this->call($method, $resource, $data);
      }

      public function call($http_verb, $resource, $data = []){}
}

But when I try to run the following...

$ra = new RestApi();
$ra->post('api/book/index');

It throws following error...

RestApi::__call() must take exactly 2 arguments

I want to get rid of duplication code so I have decided to use __call magic method for all four http verbs. Please suggest a way. Thanks.

  • 写回答

1条回答 默认 最新

  • doubo6658 2015-06-24 17:59
    关注

    Like the error message suggests, the __call magic method only takes 2 arguments: the name of the method that was requested, and an array of arguments that were passed. All this info can be found here.

    With that in mind, maybe modify your code to look more like this:

    public function __call($method, $arguments) {
        return $this->call($method, $arguments[0], $arguments[1]);
    }
    

    Assuming you implement the actual API requests with CURL (which may be a very big assumption), you could do something like this inside your call() method:

    switch (strtoupper($http_verb)) {
        case 'GET':
            // Nothing special happens here, this is CURL's default behavior
            break;
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, true);
            break;
        case 'PUT':
            curl_setopt($ch, CURLOPT_PUT, true);
            break;
    
        // Etc...
    }
    

    This is purely an example to demonstrate the conditional logic you can do with the __call() magic method. There are many libraries and frameworks that create REST wrappers like what you're trying to do. I suggest looking at one of those just as a learning experience, or maybe investigate a library like Guzzle.

    Note: Of course, with an optional second argument (like what you have above), it would be smart to do some checks to see if $arguments[1], etc. exist before blindly passing them along. Otherwise you'll get something like PHP Notice: Undefined offset: 1.

    UPDATE

    Regarding my last paragraph, @JayTaph had a good suggestion. A concise way to do this (one line, with no extra checks necessary) would be to use the following:

    public function __call($method, $arguments) {
        return this->call($method, array_shift($arguments), $arguments);
    }
    

    If only one argument gets passed, after the first element gets shifted off with array_shift(), $arguments will be an empty array, which avoids the PHP Notice: Undefined offset error I mentioned above.

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

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了