CodeIgniter 库中的方法链接
Method Chaining in CodeIgniter Library
我一直在为工作编写脚本。它是将 Slack 与 PHP 一起使用。在工作中,我们使用 CodeIgniter(悲伤的脸),所以我不得不适应,我决定把我的脚本写成一个库。
这里没有代码不起作用的问题,因为它工作正常,但我只是想知道在调用库时如何应用方法链,以便我自己和我的同事可以编码使用库时更干净。
这是我编写的库 - 我更像是一名正在接受培训的程序员,所以我的 OOP 知识有限。
<?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;
}
}
现在我在我们的 API 中使用它,它在控制器中编码,这就是我调用方法的方式:
<?php
// ...
$this->load->library('slack');
$this->slack->icon(':hotdog:');
$this->slack->send('Hello...');
正如我所说,这很好用...
不过我想要能够进行方法链接,就像这样:
<?php
// ...
$this->slack->icon(':hotdog:')->send('Hello...');
你能告诉我这是否可行以及如何实现吗?
谢谢。
正如我所见,要存档您想要的内容,您只需更改一下即可
public function icon($icon)
{
$this->setIcon($icon);
return $icon;
}
那个
public function icon($icon)
{
$this->setIcon($icon);
return $this;
}
然后你就可以做你想做的事了
$this->slack->icon(':hotdog:')->send('Hello...');
无论如何你的图标方法不需要 return $icon 你已经有了 getIcon 方法
还有你的发送方法在发出请求之前调用有效负载方法,这样应该可以工作
我一直在为工作编写脚本。它是将 Slack 与 PHP 一起使用。在工作中,我们使用 CodeIgniter(悲伤的脸),所以我不得不适应,我决定把我的脚本写成一个库。
这里没有代码不起作用的问题,因为它工作正常,但我只是想知道在调用库时如何应用方法链,以便我自己和我的同事可以编码使用库时更干净。
这是我编写的库 - 我更像是一名正在接受培训的程序员,所以我的 OOP 知识有限。
<?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;
}
}
现在我在我们的 API 中使用它,它在控制器中编码,这就是我调用方法的方式:
<?php
// ...
$this->load->library('slack');
$this->slack->icon(':hotdog:');
$this->slack->send('Hello...');
正如我所说,这很好用...
不过我想要能够进行方法链接,就像这样:
<?php
// ...
$this->slack->icon(':hotdog:')->send('Hello...');
你能告诉我这是否可行以及如何实现吗?
谢谢。
正如我所见,要存档您想要的内容,您只需更改一下即可
public function icon($icon)
{
$this->setIcon($icon);
return $icon;
}
那个
public function icon($icon)
{
$this->setIcon($icon);
return $this;
}
然后你就可以做你想做的事了
$this->slack->icon(':hotdog:')->send('Hello...');
无论如何你的图标方法不需要 return $icon 你已经有了 getIcon 方法 还有你的发送方法在发出请求之前调用有效负载方法,这样应该可以工作