这里有新鲜出炉的 PHP 设计模式,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
在 php 中有很多字符串函数,本篇文章主要介绍了 PHP 三种方式实现链式操作详解,具有一定的参考价值,有兴趣的可以了解一下。
在 php 中有很多字符串函数,例如要先过滤字符串收尾的空格,再求出其长度,一般的写法是:
- strlen(trim($str))
如果要实现类似 js 中的链式操作,比如像下面这样应该怎么写?
- $str - >trim() - >strlen()
下面分别用三种方式来实现:
方法一、使用魔法函数__call 结合 call_user_func 来实现
思想:首先定义一个字符串类 StringHelper,构造函数直接赋值 value,然后链式调用 trim() 和 strlen() 函数,通过在调用的魔法函数__call() 中使用 call_user_func 来处理调用关系,实现如下:
- <?php
- class StringHelper
- {
- private $value;
- function __construct($value)
- {
- $this->value = $value;
- }
- function __call($function, $args){
- $this->value = call_user_func($function, $this->value, $args[0]);
- return $this;
- }
- function strlen() {
- return strlen($this->value);
- }
- }
- $str = new StringHelper(" sd f 0");
- echo $str->trim('0')->strlen();
终端执行脚本:
php test.php
8
方法二、使用魔法函数__call 结合 call_user_func_array 来实现
- <?php
- class StringHelper
- {
- private $value;
- function __construct($value)
- {
- $this->value = $value;
- }
- function __call($function, $args){
- array_unshift($args, $this->value);
- $this->value = call_user_func_array($function, $args);
- return $this;
- }
- function strlen() {
- return strlen($this->value);
- }
- }
- $str = new StringHelper(" sd f 0");
- echo $str->trim('0')->strlen();
说明:
- array_unshift(array, value1, value2, value3...)
array_unshift() 函数用于向数组插入新元素。新数组的值将被插入到数组的开头。
call_user_func() 和 call_user_func_array 都是动态调用函数的方法,区别在于参数的传递方式不同。
方法三、不使用魔法函数__call 来实现
只需要修改_call() 为 trim() 函数即可:
- public
- function trim($t) {
- $this - >value = trim($this - >value, $t);
- return $this;
- }
重点在于,返回 $this 指针,方便调用后者函数。
来源: http://www.phperz.com/article/17/0806/340677.html