php chain call

WBOY
WBOYOriginal
2016-08-08 09:06:531355browse

strlen (strim ($str))This is a function to get the length of a string. How to write it as a chain operation?

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

Tips:

First implement a String class. When the object of this class is called and processed, the __call magic method is triggered, and then call_user_func is executed

Reply content:

strlen (strim ($str))This is a function to get the length of a string. How to write it as a chain operation?

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

Tips:

First implement a String class. When the object of this class is called and processed, the __call magic method is triggered, and then call_user_func is executed

The source code is as follows

<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>

It is not difficult to implement such a function, you only need to encapsulate the string. If chain calls are needed, just return $this.
Please see the following code to realize your function:

<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>

->Why do you need spaces ==, it looks so strange

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn