PHP 三种方式实现链式操作

高洛峰
Libérer: 2023-03-05 16:04:01
original
1137 Les gens l'ont consulté

在php中有很多字符串函数,例如要先过滤字符串收尾的空格,再求出其长度,一般的写法是:

strlen(trim($str))
Copier après la connexion
Copier après la connexion

如果要实现类似js中的链式操作,比如像下面这样应该怎么写?

$str->trim()->strlen()
Copier après la connexion
Copier après la connexion

下面分别用三种方式来实现:

方法一、使用魔法函数__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();
Copier après la connexion
Copier après la connexion

终端执行脚本:

php test.php 
8
Copier après la connexion
Copier après la connexion

方法二、使用魔法函数__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();
Copier après la connexion
Copier après la connexion

说明:

array_unshift(array,value1,value2,value3...)
Copier après la connexion
Copier après la connexion

array_unshift() 函数用于向数组插入新元素。新数组的值将被插入到数组的开头。

call_user_func()call_user_func_array都是动态调用函数的方法,区别在于参数的传递方式不同。

方法三、不使用魔法函数__call来实现

只需要修改_call()trim()函数即可:

public function trim($t)
{
    $this->value = trim($this->value, $t);
    return $this;
}
Copier après la connexion
Copier après la connexion

重点在于,返回$this指针,方便调用后者函数。


在php中有很多字符串函数,例如要先过滤字符串收尾的空格,再求出其长度,一般的写法是:

strlen(trim($str))
Copier après la connexion
Copier après la connexion

如果要实现类似js中的链式操作,比如像下面这样应该怎么写?

$str->trim()->strlen()
Copier après la connexion
Copier après la connexion

下面分别用三种方式来实现:

方法一、使用魔法函数__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();
Copier après la connexion
Copier après la connexion

终端执行脚本:

php test.php 
8
Copier après la connexion
Copier après la connexion

方法二、使用魔法函数__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();
Copier après la connexion
Copier après la connexion

说明:

array_unshift(array,value1,value2,value3...)
Copier après la connexion
Copier après la connexion

array_unshift() 函数用于向数组插入新元素。新数组的值将被插入到数组的开头。

call_user_func()call_user_func_array都是动态调用函数的方法,区别在于参数的传递方式不同。

方法三、不使用魔法函数__call来实现

只需要修改_call()trim()函数即可:

public function trim($t)
{
    $this->value = trim($this->value, $t);
    return $this;
}
Copier après la connexion
Copier après la connexion

重点在于,返回$this指针,方便调用后者函数。

更多PHP 三种方式实现链式操作 相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!