Home > Article > Backend Development > PHP Advanced - Function Video Tutorial Sharing
Introduction: PHP provides powerful system functions to help us complete functions, but sometimes it is not enough to just use system functions. Custom functions need to be encapsulated according to the business, "PHP Advanced - Function Video" Tutorial" explains how to customize functions and common problems in custom functions. It is a must-read course for getting started with PHP.
Course playback address: http://www.php.cn/course/417.html
The teacher’s teaching style:
The teacher’s lectures are vivid, witty, witty, and touching. A vivid metaphor is like the finishing touch, opening the door to wisdom for students; an appropriate humor brings a knowing smile to students, like drinking a glass of mellow wine, giving people aftertaste and nostalgia; a philosopher's aphorisms, cultural references Proverbs are interspersed from time to time in the narration, giving people thinking and warning.
The more difficult point in this video is the use of callback functions in PHP:
Callback functions and closures are no strangers to JS. JS can use them to complete events. mechanism to perform many complex operations. It is not commonly used in PHP. Today, let’s talk about callback functions and anonymous functions in PHP.
Callback function
Callback function: Callback (i.e. call then back will return to the main function after being called by the main function), which is passed to other code through function parameters, and a certain block can be executed Code reference.
The popular explanation is to pass the function as a parameter into another function for use; there are many functions in PHP that "require parameters as functions", such as array_map, usort, call_user_func_array, etc., they execute the passed in function and then returns the result directly to the main function. The advantage is that functions are convenient to use as values, and the code is concise and readable.
Today, I wanted to call another class method as its callback function in the class method I created. I wanted to write the name of the other class method directly in the place of the callback function, but I kept getting an error saying "Parameters" Invalid", and then I thought about whether I should try using the SELF keyword, but I still got this error. I really don’t know what to do. I checked the Chinese manual and couldn’t find a solution. Then I carefully looked at the manual on the PHP official website. This manual has the syntax to solve this problem.
class utils{
public function array_or( $input )
{
return array_reduce( $input, array('Utils','sum' ), 0 );
}
public function sum( $v, $w )
{
return $v += $w;
}
}
$uti = new utils;
$a = array(1,2,3,4,5);
echo $uti->array_or($a);
It makes me feel very strange why calling another class method is in the form of an array. It seems that using the SELF keyword is more appropriate. Hey, really I don’t know what considerations they had in designing such a syntax. But I think there must be a reason for the design like this, but I haven't realized this reason yet.
The above is the detailed content of PHP Advanced - Function Video Tutorial Sharing. For more information, please follow other related articles on the PHP Chinese website!