Strlen (strim ($ str) is a function used to obtain the length of a string. how do I write a chain operation? {Code...} prompt: first implement a String class, when processing the call method of this class object, trigger the _ call magic method, and then execute call_user_func.
strlen (strim ($str))This is a function used to obtain the length of a string. how do I write a chain operation?
$str -> trim() -> strlen()
Tip:
First, implement a String class. when processing the call method of this class object, trigger the _ call magic method, and then execute call_user_func.
Reply content:
strlen (strim ($str))This is a function used to obtain the length of a string. how do I write a chain operation?
$str -> trim() -> strlen()
Tip:
First, implement a String class. when processing the call method of this class object, trigger the _ call magic method, and then execute call_user_func.
The source code is as follows:
* @ Link http://flc.ren */Class Str {/*** string * @ var [type] */protected $ string; /*** supported functions * @ var array */protected $ methods = ['trim', 'strlen']; /*** initialize * @ param [type] $ string [description] */public function _ construct ($ string) {$ this-> string = $ string ;} /*** Singleton mode ** @ 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 );} /*** output * @ return string */public function response () {return $ this-> string ;} /*** mode method * @ 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-> stri Ng) ;}} return $ this ;}// DEMOecho Str: getInstance ('1970, 123123 ')-> trim ()-> strlen () -> response ();?>
Str = $ str;} public function trim () {$ this-> str = trim ($ this-> str); return $ this;} public function strlen () {return strlen ($ this-> str) ;}$ str = new str ('think, do not reach out to the dang'); var_dump ($ str-> trim () -> strlen (); die;
Output:
php test.phpint(25)
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 ('Is the subject a contact? '); 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 );
Does the subject reach out to the party? 28. Is it a hand-held party?
It is not difficult to implement such a function. you only need to encapsulate the string. If you need to perform a chained call, you can return $ this.
See the following code to implement your functions:
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 ();?>
-> Why does it look strange to use space =?