我所有的 API 调用都是重复的代码,是否可以在 PHP 中创建一个使用 Fluent Interface 的 API 客户端?

问题描述 投票:0回答:1

我正在使用一个 SOAP API,我创建的每个方法都是相同的,除了一个与函数名称和参数匹配的字符串。我觉得它非常适合 Fluent 界面。我将如何着手在 PHP 中制作它?如果方法名称不存在,我还能运行一些东西吗?

   public function getFacilityIds() {
        $params = array(
            'sessionKey' => $this->sessionKey
        );
        return $this->client->__soapCall('getFacilityIds', $params);
    }

    public function getFacilityInformation() {
        $params = array(
            'sessionKey' => $this->sessionKey
        );
        return $this->client->__soapCall('getFacilityInformation', $params);
    }

我认为这将是大部分代码(不包括 args),但我不知道我会命名该函数以使其工作。

   public function ??? () {
        $params = array(
            'sessionKey' => $this->sessionKey
        );
        return $this->client->__soapCall(__FUNCTION__, $params);
    }
php rest fluent
1个回答
-2
投票

看起来 __call 是我正在寻找的答案。我一直遇到的缺陷是建议在 IDE 中不起作用,如何在使用此方法使用 API 的同时让代码完成在 VSCode 中工作?

© www.soinside.com 2019 - 2024. All rights reserved.