douxiza9868 2015-06-24 18:06
浏览 38

将API调用的结果传递给类

I've made my own Wrapper for an API I'm working with, Now I have a class where uses can register and I need to results of that API call in the whole class.

So I'm wondering if it's possible to have the result object that the API returns as an object to work with and pass to other methods

Here's my code, I added inline comments to clarify the issue

class RegisterController extends Controller {

    private $company;

    public function __construct()
    {
        // This is not the object I want, It's the class that communicates with the API
        $this->company = new Teamleader\Company;
    }

    public function index($id = null)
    {
        // I'm trying to set the class to the result of the API call ( returns an StdObject )
        $this->company = $this->company->get($id);

        // Still returns the original object, not the result of the API call
        var_dump($this->company);
    }

    public function register()
    {
        // Also returns the original object
        dd($this->company);
    }

}

Thank you very much, a confused developer.

  • 写回答

1条回答 默认 最新

  • dongwalun2507 2015-06-24 18:46
    关注

    Instead of trying to overwrite the api wrapper, just set it as some custom response object.

    namespace Teamleader;
    
    class CompanyResponse {
    
        protected $data = null;
    
        public function __construct(\stdClass $data)
        {
            $this->data = $data;
        }
    
        // Whatever other methods you want to be in the API response
    }
    

    And then in the get method in Teamleader\Company you will return new CompanyResponse($data)

    In the index method in RegisterController, set the response object as a different class property. There really isn't much of a reason to overwrite the API wrapper you created.

    $this->companyResponse = $this->company->get($id);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序