简体   繁体   English

知道结束php oop中方法链接的意义

[英]know the point of ending the method chaining in php oop

Can you help me? 你能帮助我吗?

please let me know the point of ending the chain? 请让我知道结束连锁店的意义吗?

for example: 例如:

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'

How can I write function? 如何编写函数?

The static method string is a factory method. 静态方法string是工厂方法。 It is used to create a new instance of the class. 它用于创建类的新实例。 The rest of the methods must return the object itself to maintain the chainability. 其余方法必须返回对象本身以保持可链接性。 Also, by implementing the __toString method, the object can be printed or concatenated to a 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'

You might want to perform different operations on an input string by chaining as many methods as you want. 您可能希望通过链接任意数量的方法来对输入字符串执行不同的操作。 Then you can have a method that when called returns the final content of the string after manipulation. 然后,您可以拥有一个方法,该方法在被调用时会在操作后返回字符串的最终内容。 You might want to store the result of this operation in a variable without necessarily having to echo it to the browser. 您可能需要将此操作的结果存储在变量中,而不必将其回显到浏览器。 Here is a way you can achieve this 这是一种可以实现这一目标的方法

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

With this class, you don't have to be forced to echo in order to get the contents of the final string, just store it in a variable and use as you wish. 使用此类,您不必为了获得最终字符串的内容而被强制echo ,只需将其存储在变量中即可使用。 This class enables you to chain methods and then call the last get() to terminate the chain and return the final string value. 此类使您可以链接方法,然后调用最后的get()来终止链并返回最终的字符串值。 You can call the method in any order A::replace()->sub()->get() or A::sub()->replace()->get() calling the get() as the last to terminate the chain. 您可以按任意顺序调用方法A::replace()->sub()->get()A::sub()->replace()->get()get()作为最后一个调用终止链。 You only need to pass the $string value once with the first method you call and it's value would be preserved for other chained methods. 您只需使用调用的第一个方法传递一次$string值,该值将保留给其他链接方法。 So for example, you would do this A::replace($string, $search, $replace)->sub($start, $count)->get() or A::sub($string, $start, $count)->replace($search, $replace)->get() 因此,例如,您可以执行A::replace($string, $search, $replace)->sub($start, $count)->get()A::sub($string, $start, $count)->replace($search, $replace)->get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM