Broad Network


PHP Callback and Anonymous Functions

Advanced Features of PHP Function – Part 1

Foreword: In this part of the series, I explain PHP Callback function.

By: Chrysanthus Date Published: 26 Jan 2014

Introduction

This is part 1 of my series, Advanced Features of PHP Function. In this part of the series, I explain PHP Callback function. This is a tutorial.

Note: the output of all the code samples in this series are sent to the browser.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or just want to comment, contact me at forchatrans@yahoo.com .

Pre-Knowledge
There are certain topics you need to have covered before you can understand this series. Click the link titled, “PHP Course” below to know what you should have studied before reaching here. This series is part of my volume, PHP Course.

Meaning of Callback Function
A callback function can be a user defined function or a built-in function. A user-defined function is a function with a name that you, the programmer define. A built-in function is a function, which comes with the PHP installation. You use a callback function, by passing the name of the callback function as a parameter (argument) to another function.

Callback Function without Parameters
Let us look at a callback function that does not have parameters (does not take arguments). Read and try the following code:

<?php
    
    function toCB()
        {
            $ret = 2 * 3;
            return $ret;
        }

    function func($fixed, $fn)
        {
            $rt = $fixed + $fn;
            return $rt;
        }

    $var = func(4, toCB());

    echo $var;

?>

The output is,

    10

The function with the name, toCB() is the callback function. This function multiplies the numbers 2 and 3 and returns the result of 6. A callback function is a normal function, but it is intended to be used inside the parameter (argument) list of another function. The name of the callback function would be sent as an argument of the other function.

The name of the other function in the code that uses the callback function is, func(). This function name and parameters is:

    func($fixed, $fn)

This function, as defined above adds the parameters, $fixed and $fn together. At the func() function call, the argument for $fixed is just a number. At the function call, the argument for $fn can be a number literal or a return value of a function. In the case of the return value of a function, you can just type the function name and the parentheses, as the argument.  The function call for func() above is:

    func(4, toCB())

Read the above code again, where $fn represents a callback function that returns a number.

Callback Function with Parameters
The following code does the same work as the above, but the callback function has parameters (receives arguments):

<?php
    
    function toCB($no1, $no2)
        {
            $ret = $no1 * $no2;
            return $ret;
        }

    function func($fixed, $fn)
        {
            $rt = $fixed + $fn;
            return $rt;
        }

    $var = func(4, toCB(2,3));

    echo $var;

?>

Note how the callback function has been defined and note how the other function has been defined and called.

So, a callback function is a function passed as argument to some other function.

Language Constructs and Callback Functions

Any built-in or user-defined function can be used as a callback function. However, language constructs, which are not really functions, cannot be used as callback functions. Examples of language constructs are: array(), echo(), empty(), eval(), exit(), isset(), list(), print() or unset(). These cannot be used as callback functions.

Built-in Calling Functions
As of today, the built-in calling functions that come with the PHP installation, do not really access callback functions as explained above. They access in a slightly different way. With these built-in calling functions, you type the callback function name as argument, as a string, in quotes.

A method is the function of an object or a class. In order to pass a method to these built-in calling functions, you have to pass but an indexed array of two elements. In the case of an instantiated object, index 0 has the name of the object and index 1 has the name of the method. In the case of a static method, index 0 has the name of the class and index 1 has the name of the method.

Anonymous Function
An anonymous function is a function that has no name. An anonymous function is also called a closure. An anonymous function can be assigned to a variable. In the assignment statement you end the function with a semicolon. The variable can then be used in the place of a callback function. Read and try the following code:

<?php
    
    $va = function($no1, $no2)
        {
            $ret = $no1 * $no2;
            return $ret;
        };

    function func($fixed, $fn)
        {
            $rt = $fixed + $fn;
            return $rt;
        }

    $var = func(4, $va(2,3));

    echo $var;

?>

The output is:

    10

In the code, the anonymous function has been assigned to the variable, $va. The first code segment, which has the anonymous function, is an assignment statement and ends with a semicolon. To call such an assigned anonymous function, type the variable followed by parentheses. If there are any arguments, place them in the parentheses, as in the call:

    $va(2,3)

In the code, “$va(2,3)” has been used as callback.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message