首頁  >  文章  >  後端開發  >  php的鍊式調用

php的鍊式調用

WBOY
WBOY原創
2016-08-08 09:06:531310瀏覽

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>

->為什麼要空格 = =,看起來好奇怪

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn