Help understand a code with Currying feature

0

I'm studying a tutorial on the following link:

Roll Your Own Templating System in PHP

The author in the code part did not format properly but is not complex to adjust. So I've been doing it, yet in a part I've grabbed. I did not understand the explanation of Currying and I could not correct the formatting of the code in that part:

Step 7 Get Ready for Template Tag Replacement: Currying

    /** * A currying function * * Currying allows a function to be called incrementally. This means that if * a function accepts two arguments, it can be curried with only one * argument supplied, 
which returns a new function that will accept the * remaining argument and return the output of the original curried function * using the two supplied parameters. * * Example: 

 function add($a, $b) { return $a + $b; } 

 $func = $this->_curry('add', 2); 

 $func2 = $func(1); // Stores 1 as the first argument of add() 

 echo $func2(2); // Executes add() with 2 as the second arg and outputs 3 

 * @param string $function The name of the function to curry * @param int $num_args The number of arguments the function accepts * @return mixed Function or return of the curried function */
private function _curry( $function, $num_args ) { return create_function('', " // Store the passed arguments in an array \$args = func_get_args(); 

 // Execute the function if the right number of arguments were passed if( count(\$args)>=$num_args ) { return call_user_func_array('$function', \$args); } 

 // Export the function arguments as executable PHP code \$args = var_export(\$args, 1); 

 // Return a new function with the arguments stored otherwise return create_function('',' \$a = func_get_args(); \$z = ' . \$args . '; \$a = array_merge(\$z,\$a); return 
call_user_func_array(\'$function\', \$a); '); "); } 

It's written that way. At first it made no sense to me, 1) I did not understand this curry method; 2) The code is with some changes that do not close at the end.

NOTE: I think 'is' (single quotes)

Within another method the _curry function was called as follows:

// Curry the function that will replace the tags with entry data 
$callback = $this->_curry('Template::replace_tags', 2); 

And the replace_tags method is written like this:

 /** * Replaces template tags with the corresponding entry data * 
  * * @param string $entry A serialized entry object 
  * * @param array $params Parameters for replacement 
  * * @param array $matches The match array from preg_replace_callback() 
  * * @return string The replaced template value */ 
 public static function replace_tags($entry, $matches) { 
    // Unserialize the object 
    $entry = unserialize($entry); 
    // Make sure the template tag has a matching array element 
    if( property_exists($entry, $matches[1]) ) {
        return $entry->{$matches[1]};
    // Otherwise, simply return the tag as is    
    }else{
        return "{".$matches[1]."}";
    }
 } 
    
asked by anonymous 14.01.2018 / 23:52

0 answers