Heim  >  Artikel  >  Backend-Entwicklung  >  php的链式调用

php的链式调用

WBOY
WBOYOriginal
2016-08-08 09:06:531310Durchsuche

strlen (strim ($str))这是获取字符串长度的函数,如何写成链式操作?

<code>$str -> trim() -> strlen()</code>

提示:

先实现一个String类,对这个类的对象调用方法进行处理时,触发__call魔术方法,接着执行call_user_func即可

回复内容:

strlen (strim ($str))这是获取字符串长度的函数,如何写成链式操作?

<code>$str -> trim() -> strlen()</code>

提示:

先实现一个String类,对这个类的对象调用方法进行处理时,触发__call魔术方法,接着执行call_user_func即可

源码如下

<code class="php"><?php /**
 * 字符串处理
 *
 * @author Flc <2016-08-04 23:39:41>
 * @link http://flc.ren
 */
class Str
{
    /**
     * 字符串
     * @var [type]
     */
    protected $string;

    /**
     * 支持的函数
     * @var array
     */
    protected $methods = ['trim', 'strlen'];

    /**
     * 初始化
     * @param [type] $string [description]
     */
    public function __construct($string)
    {
        $this->string = $string;
    }

    /**
     * 单例模式
     * @param  string $string 字符串
     * @return Object:Str         
     */
    public static function getInstance($string)
    {
        static $_instances = [];

        if (isset($_instances[$string]))
            return $_instances[$string];

        return $_instances[$string] = new self($string);
    }

    /**
     * 输出
     * @return string 
     */
    public function response()
    {
        return $this->string;
    }

    /**
     * 模式方法
     * @param  string $method 方法
     * @return Object:Str         
     */
    public function __call($method, $args)
    {
        if (in_array($method, $this->methods)) {
            if (function_exists($method)) {
                $this->string = call_user_func($method, $this->string);
            }
        }

        return $this;
    }
}

// DEMO
echo Str::getInstance(' 123123 123')->trim()->strlen()->response();
?></code>

<code><?php /**
 * Created by PhpStorm.
 * User: hongxu
 * Date: 16/8/4
 * Time: 13:21
 */

class str {

    private $str = '';

    /**
     * str constructor.
     */
    public function __construct($str) {

        $this->str = $str;
    }

    public function trim() {
        $this->str = trim($this->str);
        return $this;
    }

    public function strlen() {
        return strlen($this->str);
    }
}

$str = new str('要思考,不做伸手党');

var_dump($str->trim()->strlen());die;</code>

output:

<code>php test.php
int(25)
</code>

<code class="php"><?php class Str {
    private $value = '';

    public function __construct($str) {
        $this->value = $str;
    }

    public function __call($name, $args) {
        if (function_exists($name)) {
            array_unshift($args, $this->value);
            $value = call_user_func_array($name, $args);
            if (is_string($value)) {
                return new static($value);
            } else {
                return $value;
            }
        }
    }

    public function __toString() {
        return $this->value;
    }
}

$str = new Str('  题主是不是个伸手党?  ');

echo($str->trim() . PHP_EOL);
echo($str->trim()->strlen() . PHP_EOL);
$trim_str = $str->trim();
echo($trim_str->substr(0, $trim_str->strlen() - 1) . PHP_EOL);</code>
<code>题主是不是个伸手党?
28
题主是不是个伸手党</code>

要实现这样的功能并不难,只需要对string进行封装一下。如果需要链式调用,则可台返回$this即可。
请看如下代码,即可实现您的功能:

<code><?php //演示链式调用

class MyString{
    private $string;
    
    function MyString($str){
        $this->string=$str;
    }
    
    function len(){
        return strlen($this->string);
    }
    
    function _trim(){
        $this->string=trim($this->string);
        return $this;
    }
}

$d=new MyString(' sdfsdf  ');
echo $d->_trim()->len();
?></code>

->为什么要空格 = =,看上去好奇怪

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn