down123321123 2018-04-21 15:15
浏览 57
已采纳

在CodeIgniter库中链接的方法

I've been working on a script for work. It's to use Slack with PHP. At work we use CodeIgniter (sad face) so I have to accommodate and I decided to write my script as a Library.

There isn't an issue here in that the code doesn't work, as it works fine, but I am just interested to know how I can apply method chaining when calling the library so myself and my colleagues can code cleaner when we use the library.

Here's the Library I wrote - I'm more of a programmer in-training so my OOP knowledge is limited.

<?php defined('BASEPATH') OR exit('No direct script access allowed');

include('../vendor/autoload.php');

use GuzzleHttp\Client;

class Slack {

    protected $ci;

    private $channel;

    private $endpoint;

    private $icon;

    private $username;

    public function __construct()
    {
        $this->ci =& get_instance();
        $this->ci->config->load('slack');

        $this->baseUri  = $this->ci->config->item('base_uri');
        $this->channel  = $this->ci->config->item('channel');
        $this->endpoint = $this->ci->config->item('endpoint');
        $this->icon     = $this->ci->config->item('icon');
        $this->username = $this->ci->config->item('username');

        $this->client = new Client([
            'base_uri' => $this->baseUri
        ]);
    }

    public function getChannel()
    {
        return $this->channel;
    }

    public function setChannel($channel)
    {
        $this->channel = $channel;

        return $this;
    }

    public function getEndpoint()
    {
        return $this->endpoint;
    }

    public function setEndpoint($endpoint)
    {
        $this->endpoint = $endpoint;

        return $this;
    }

    public function getIcon()
    {
        return $this->icon;
    }

    public function setIcon($icon)
    {
        (mb_substr($icon, 0, 1) == ':')
            ? $this->iconType = 'icon_emoji'
            : $this->iconType = 'icon_url';

        $this->icon = $icon;

        return $this;
    }

    public function getIconType()
    {
        return ($this->iconType) ? $this->iconType : 'icon_emoji';
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    public function to($channel)
    {
        $this->setChannel($channel);

        return $channel;
    }

    public function from($username)
    {
        $this->setUsername($username);

        return $username;
    }

    public function icon($icon)
    {
        $this->setIcon($icon);

        return $icon;
    }

    public function payload($text)
    {
        $payload = [
            'channel' => $this->getChannel(),
            $this->getIconType() => $this->getIcon(),
            'link_names' => 1,
            'text' => $text,
            'username' => $this->getUsername(),
        ];

        return $payload;
    }

    public function send($text)
    {
        $payload = json_encode($this->payload($text));

        $this->client->post($this->getEndpoint(), [
            'body' => $payload
        ]);

        return $this;
    }

}

Now I'm using this in our API, which is coded in a Controller, and this is how I am calling the methods:

<?php
// ...
$this->load->library('slack');

$this->slack->icon(':hotdog:');
$this->slack->send('Hello...');

As I said, this works fine...

I wanted though, to be able to do method chaining, like so:

<?php
// ...
$this->slack->icon(':hotdog:')->send('Hello...');

Can you tell me if this is possible and how to achieve it?

Thank you.

  • 写回答

1条回答 默认 最新

  • douyun1860 2018-04-21 15:30
    关注

    As i can see to archive what you want you just need to change that

    public function icon($icon)
        {
            $this->setIcon($icon);
    
            return $icon;
        }
    

    to that

    public function icon($icon)
    {
        $this->setIcon($icon);
    
        return $this;
    }
    

    and then you will be able to do what you want

    $this->slack->icon(':hotdog:')->send('Hello...');
    

    Anyway your icon method no need to return $icon you already have getIcon method also your send method calling payload method before make request so that should work

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料