知道在php oop中结束链接方法的重点

时间:2015-12-16 07:46:13

标签: php oop method-chaining

你能帮助我吗?

请让我知道结束链条的意义?

例如:

class A {
     // some static function
}
A::string()->hash() // return 'abcd'
A::string()->hash()->replace('a','z') // return 'zbcd'
A::string()->hash()->replace('a','z')->sub(0,2) // return 'zb'

我该如何写函数?

2 个答案:

答案 0 :(得分:1)

静态方法string是一种工厂方法。它用于创建类的新实例。其余方法必须返回对象本身以保持可链接性。此外,通过实现__toString方法,可以打印对象或将其连接到字符串。

class A {
    protected $string;

    public function __construct($string = null)
    {
        $this->string = $string;
    }
    public static function string($string = 'abcd')
    {
        return new self($string);
    }
    public function hash()
    {
        return $this;
    }
    public function replace($search, $replace)
    {
        $this->string = str_replace($search, $replace, $this->string);
        return $this;
    }
    public function sub($start, $count)
    {
        $this->string = substr($this->string, $start, $count);
        return $this;
    }
    public function toString()
    {
        return $this->string;
    }
    public function __toString()
    {
        return $this->toString();
    }
}

echo A::string()->hash(); // prints 'abcd'
echo A::string()->hash()->replace('a','z'); // prints 'zbcd'
echo A::string()->hash()->replace('a','z')->sub(0,2); // prints 'zb'

答案 1 :(得分:0)

您可能希望通过链接任意数量的方法对输入字符串执行不同的操作。然后你可以有一个方法,当被调用时,在操作后返回字符串的最终内容。您可能希望将此操作的结果存储在变量中,而不必将其回显到浏览器。 这是一种可以实现此目的的方法

<?php

class A {

    /**
    *@var string The string to manipulate
    */
    private static $string;

    /**
    *This method replaces parts of a string with another string value.
    *@param string $string The string whose content is to be searched and replaced
    *@param string $search The string to search for in the $string
    *@param string $replace The string to replace with the $search string
    *@param \Object This static class
    */
    public static  function replace($string = null, $search, $replace)
    {
        //check if input string is provided and set the value of the string property
        if($string !== null) self::$string = $string;

        //evaluate and set the value of the string property
        self::$string = str_replace($search, $replace, self::$string);

        //return this static class
        return new static;
    }

    /**
    *This method returns part of the string.
    *@param string $string The string whose part is to be returned
    *@param int $start Where to start truncating from
    *@param int $count The number of characters to return from the starting point
    *@param \Object This static class
    */
    public static  function sub($string = null, $start, $count)
    {
        //check if input string is provided and set the value of the string property
        if($string !== null) self::$string = $string;

        //evaluate and set the value of the string property
        self::$string = substr(self::$string, $start, $count );

        //return this static class
        return new static;
    }

    /**
    *This method returns the final string after manipulation.
    *@param null
    *@return string The value of $string property
    */
    public static function get()
    {
       //return the contents of the string property 
       return self::$string;
    }

}

//here are the options you can have
$string = A::replace($string, $search, $replace)->sub($start, $count)->get(); //replace, get substring and return string
$string = A::replace($string, $search, $replace)->get(); //replace and return string
$string = A::sub($string, $start, $count)->replace($search, $replace)->get(); //get substring, replace and return string
$string = A::sub($string, $start, $count)->get(); //get substring and return string
?>

使用此类,您不必强制使用echo来获取最终字符串的内容,只需将其存储在变量中并按照您的意愿使用即可。 此类使您能够链接方法,然后调用最后一个get()来终止链并返回最终的字符串值。您可以按任意顺序A::replace()->sub()->get()A::sub()->replace()->get()调用该方法,将get()作为最后一个终止链。您只需要使用您调用的第一个方法传递$string值一次,并为其他链接方法保留其值。例如,您可以执行此操作A::replace($string, $search, $replace)->sub($start, $count)->get()A::sub($string, $start, $count)->replace($search, $replace)->get()