PHP Inner Functions And Closure
Written by Mike James   
Friday, 07 January 2022
Article Index
PHP Inner Functions And Closure
Lifetimes
Object-oriented PHP
Anonymous functions can be local

PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP functions work and how you might be writing one even if you don't know you are...

 

Introduction to PHP

phpcover

Contents

  1. Getting started
    Getting Started With NetBeans PHP - Local Projects
  2. What PHP Does
  3. PHP Variables and Expressions for Complete Beginners
  4. PHP Control Structures 1 - if and else
  5. PHP Control Structures 2 - switch and elseif
  6. PHP loops
  7. Advanced loops
  8. Functions
  9. Ten minutes to PHP objects
  10. PHP Inner Functions And Closure
  11. NetBeans Remote Projects and debugging

Normally you will hear programmers familiar with other languages complaining about PHP's approach to inner functions. They often come to the conclusion that they are mostly a waste of time because they aren't local, they don't implement closure and they if the outer function is executed more than once they cause an error.

In fact as long as you understand how they work they can be very useful and without them your code could be very messy indeed.

When you put them together with anonymous functions, added in PHP 5.3.0, you have something that is almost, but not quite, the equal of what other languages offer.

But first let's look at the basics of PHP functions. 

Functions are global

A PHP function is simple to define:

function MyFunction()
{
 instructions that make up the function
}

You can include parameters and a return statement.

The important point is that any function defined in this way has global scope. It can be called from anywhere within the entire program including from within another function and even from within a class or an instance of a class.

For example, if we define the function:

function MyFunction()
{
 echo("My Function");
}

Then it can be used from within a class:

class MyClass
{
 public function MyMethod()
 {
  MyFunction();
 }
}

and when the method is called

$MyObject=new MyClass();
$MyObject->MyMethod();

MyFunction is called.

This might seem perfectly normal to you as a PHP programmer but most other object oriented languages don't even allow you to define a function outside of a class, i.e. all functions are methods, so the idea of having global functions doesn't arise.

Using functions in this way is not a good idea even though it can be rationalised as providing "helper" functions to the objects you create. In general the functionality of a class should always be contained within the class and not made available by a global entity.

You can argue that string functions are an example of utility functions that should be global, for example count_chars is a global function that returns the number of characters in a string, but is just an indication that you are not being object oriented enough. If a function does something to a string, i.e. it's a string operation, then it should be part of a string class.

To find any convincing examples of functions that should be global you have to look at either operations that involve more than one type - which class should the function belong to - or a where a single function works with multiple types.  Working with multiple types is usually handled in fully object oriented languages by using generics but loosely typed languages such as PHP just allow you to ignore the problems of type and write a global function. It sort of works and avoids introducing a complicated idea into the language.

Inner functions

If you define a variable within a function it is  local to that function. You can also define a function within another function and logically this too should be a local variable but recall the rule that all functions are global.

In PHP inner functions are global and hence behave in the same way as if they had been declared outside of any containing function.

What this means is that inner functions are useless.

As they are global they have the same context as a function declared in the usual way and this in turn means that they cannot support any of the clever tricks that other languages provide such as closure.

Closure is where an inner function has access to the local variables of the function that contains it - even if the outer function no longer exists. As inner functions are global they have no privileged access to the local variables of the functions in which they are declared. Also as functions are global they never go out of scope and so are never destroyed.

Thus PHP functions miss two things that make closure possible and a useful feature and both are due to the the functions being global.

 

Banner

 

To be clear: an inner function behaves in exactly the same way as if it had been declared outside of its containing function. It has no access to the local variables of its containing function and concepts such as closure simply do not apply. 



Last Updated ( Friday, 07 January 2022 )