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.
This class enables you to chain methods and then call the last get() to terminate the chain and return the final string value. 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. 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. 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()