Principles of callback functions and PHP instances

Source: Internet
Author: User
Principle of callback function and PHP instance background: In a recent development project, you have to call the service before starting a series of query activities. after thinking for a long time, I was reminded by my colleagues that, use the callback function to solve the problem. Here, I will explain the principles and implementation of callback functions in PHP.


1. what is Callback?

There are always some interfaces between software modules. in terms of calling methods, they can be divided into three types: synchronous call, callback and Asynchronous call. Synchronous call is a blocking call. the caller must wait for the execution of the other party to complete before returning. it is a one-way call. callback is a two-way call mode, that is, the called party also calls the other party's interface when the interface is called. Asynchronous call is a mechanism similar to messages or events, but its call direction is the opposite, when an interface service receives a message or an event, it proactively notifies the customer (that is, the customer's interface ). Callback and Asynchronous call are closely related. generally, callback is used to register asynchronous messages and message notifications are implemented through asynchronous calls. Synchronous calling is the simplest of the three, and callback is often the basis of asynchronous calling. Therefore, we will focus on the implementation of callback mechanisms in different software architectures.

For different types of languages (such as structured and object languages), platforms (Win32, JDK), or architectures (such as CORBA, DCOM, and WebService), the interaction between customers and services is not synchronized, A certain asynchronous notification mechanism is required for the service provider (or interface provider) to actively notify the customer in some cases, and callback is the simplest way to implement asynchronization. For general structured languages, callback can be implemented through callback functions. A callback function is also a function or process, but it is a special function implemented by the caller for the caller.
In an object-oriented language, callback is implemented through an interface or abstract class. we turn the class implementing this interface into a callback class, and the object of the callback class into a callback object. For Object languages such as C ++ or Object Pascal that are compatible with the process features, they not only provide callback objects, callback methods, and other features, but also support the callback function mechanism of the process language. The message mechanism on the Windows platform can also be seen as an application of callback. We use the interface provided by the system to register the message processing function (that is, the callback function) to receive and process messages. Because Windows APIs are built in C language, we can think of them as a special case of callback functions. For the distributed component proxy system (CORBA), asynchronous processing involves multiple methods, such as callback, event service, and notification service. Event Service and notification service are the standard services used by CORBA to process asynchronous messages. They are mainly responsible for message processing, distribution, and maintenance. Some simple asynchronous processing processes can be implemented through the callback mechanism.

2. implementation of PHP callback functions


2.1 Global function callback

A global function is a function defined by a function that is not included in any object or class. See the following example.
Function fnCallBack ($ msg1, $ msg2)
{
Echo 'msg1: '. $ msg1;
Echo"
\ N ";
Echo 'msg2: '. $ msg2;

}


$ FnName = "fnCallBack"; // method name
$ Params = array ('hello', 'World'); // the value passed to the parameter.

Call_user_func_array ($ fnName, $ params );

Result:


Code description:
The PHP built-in function call_user_func_array is used for calling. Call_user_func_array has two parameters. The 1st parameters are a string indicating the name of the function to be called. The 2nd parameters are an array representing the parameter list, the functions to be called are passed in order.

2.2 Class static method callback
What if the callback method is a static method of the class? We can still use the built-in call_user_func_array method of PHP to call it. See the example below:
Sample code:
Class MyClass
{
Public static function fnCallBack ($ msg1, $ msg2)
{
Echo 'msg1: '. $ msg1;
Echo"
\ N ";
Echo 'msg2: '. $ msg2;
}
}


$ ClassName = 'myclass'; // class name
$ FnName = "fnCallBack"; // method name in the class
$ Params = array ('hello', 'World'); // the value passed to the parameter.
Call_user_func_array (array ($ className, $ fnName), $ params );


Result:


Code description:
This code is similar to the code of the 1st method. we pass the class name (MyClass) as the 1st parameter of call_user_func_array to implement the callback of the static method of the class. Note that the 1st parameters of call_user_func_array are an array, the 1st elements of the array are the class names, and the second element is the name of the function to be called.

If I use this method to call a non-static method of a class (that is, to remove the static method), what will happen? See the following code.
Class MyClass
{
Public function fnCallBack ($ msg1, $ msg2)
{
Echo 'msg1: '. $ msg1;
Echo"
\ N ";
Echo 'msg2: '. $ msg2;
}
}
$ ClassName = 'myclass ';
$ FnName = "fnCallBack ";
$ Params = array ('hello', 'World ');
Call_user_func_array (array ($ className, $ fnName), $ params );
The final running result is the same as the original one.


2.3 Object method callback

I tried the method in the original string format first, as shown below:
Class MyClass
{
Private $ name = 'abc ';
Public function fnCallBack ($ msg1 = 'default msg1', $ msg2 = 'default msg ')
{
Echo 'object name: '. $ this-> name;
Echo"
\ N ";
Echo 'msg1: '. $ msg1;
Echo"
\ N ";
Echo 'msg2: '. $ msg2;
}
}
$ Myobj = new MyClass ();
$ FnName = "fnCallBack ";
$ Params = array ('hello', 'World ');
$ Myobj-> $ fnName ();

Result:


The call was successful. but how can we pass the params parameter to this method? if we directly transfer params, it will be used as a parameter. how can we split the params and pass it in? I checked the PHP manual and found the create_function function. this method can be used to create an anonymous function with a string. well, you have a good idea. you can create an anonymous function, in this anonymous function, call our callback function and pass the parameters in.
I will create an anonymous function anonymous manually. in this function, call the callback function using the method obtained in the previous interview, as shown below:
Class MyClass
{
Private $ name = 'abc ';
Public function fnCallBack ($ msg1 = 'default msg1', $ msg2 = 'default msg ')
{
Echo 'object name: '. $ this-> name;
Echo"
\ N ";
Echo 'msg1: '. $ msg1;
Echo"
\ N ";
Echo 'msg2: '. $ msg2;
}
}
$ Myobj = new MyClass ();
$ FnName = "fnCallBack ";
$ Params = array ('hello', 'World ');
// Construct an anonymous function
Function anonymous ()
{
Global $ myobj;
Global $ fnName;
Global $ params;
$ Myobj-> $ fnName ($ params [0], $ params [1]);
}
Anonymous ();

Succeeded. The effect is as follows:

References:
Http://myceo.blog.51cto.com/2340655/725411/
Http://blog.chinaunix.net/uid-20684384-id-1895266.html
Http://segmentfault.com/a/1190000002901770

Http://www.abc3210.com/2012/phper_07/php-callback.shtml

Thank you very much for the above references and bloggers!

Copyright Statement: Hello, we are very happy to meet you on CSDN! Hope to make friends with you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.