/* * ** Function name: * val2str * Reference: http://www.php.net/manual/en/function.create-function.php#103080 ** Description: * Change variable value to PhPCode** Parameters: * $ mixed [mixed]: variable value ** returns: * [String] PHP code ** Demo: * $ object = new stdclass; * $ object-> Arg = 'object'; * echo val2str ($ object); ** $ CLS = new stdclass (); * $ CLS-> key = 'value '; * $ array = (array) $ CLS; * echo val2str ($ array); ** $ temp = array (* 'int' => 100, * 'double' => 3.14, * 'string' => 'string test', * 'array' => array ('test21' => 'ok21 ', 'test22' => 'ok22'), * 'object' => (object) array ('test31' => 'ok31', 'test32' => 'ok32 '), * 'bool '=> true, * 'null' => null, *); * eval (sprintf (' $ test = % 1 $ s ;', val2str ($ temp); * print_r ($ test ); */ Function Val2str ( $ Mixed ){ // Get variable name $ Var_name ='' ; // Get variable value Switch ( GetType ( $ Mixed )){ Case 'String ':Return Sprintf ('\' % 1 $ s \'', $ Mixed ); Case 'Object ': Case 'Array ': $ Isobject = False ; If ( Is_object ( $ Mixed )){ $ Isobject =True ; $ Mixed = Get_object_vars ( $ Mixed );} $ Delimiter = ',' ; $ Out ='' ; Foreach ( $ Mixed As $ Key =>$ Value ){ $ Fun = _ Function __ ; $ Out . = Sprintf ('% 1 $ s \' % 2 $ s \ '=> % 3 $ s ', $ Delimiter , $ Key , $ Fun ( $ Value ));} If (! Empty ( $ Out )){ // Delete the previous $ delimiter $ Out = Substr ( $ Out , Strlen ( $ Delimiter ));} $ Out = $ Isobject ? Sprintf ('(Object) array (% 1 $ s )', $ Out ): Sprintf ('Array (% 1 $ s )', $ Out ); Return $ Out ; Case 'Boolean ': Return $ Mixed ? 'True': 'false' ; Case 'Null ': Return 'Null' ; // Case 'integer': // case 'double': // case 'resource ': Default : Return $ Mixed ;}} /* * ** Function name: * createfunctionalias * Reference: http://www.php.net/manual/en/function.create-function.php#103080 ** Description: * create function alias ** parameters: * $ function_name [String]: function name (must already exist) * $ alias_name [String]: new alias (must not exist) ** returns: * [bool] Return result * */ Function Createfunctionalias ($ Function_name , $ Alias_name ){ If ( Function_exists ( $ Alias_name )) Return False ; $ RF = New Reflectionfunction ( $ Function_name ); $ Fproto = $ Alias_name .'(' ; $ Fcall = $ Function_name .'(' ; $ Need_comma = False ; Foreach ( $ RF -> Getparameters () As $ Param ){ If ($ Need_comma ){ $ Fproto . = ',' ; $ Fcall . = ',' ;} $ Fproto . = '$ '. $ Param -> Getname (); $ Fcall . = '$ '. $ Param -> Getname (); If ($ Param -> Isoptional ()&& $ Param -> Isdefaultvalueavailable ()){ $ Val = $ Param -> Getdefaultvalue (); $ Fproto . = '. Val2str ( $ Val );} $ Need_comma = True ;} $ Fproto . = ')' ; $ Fcall . = ')' ; $ F = Sprintf ('Function % 1 $ s {return % 2 $ s ;}', $ Fproto , $ Fcall ); Eval ( $ F ); Return True ;} // Create a createfunctionalias alias If (! Function_exists ('Function _ alias' )){ Function Function_alias ( $ Function_name , $ Alias_name ){ Return Createfunctionalias ( $ Function_name , $ Alias_name );}}