PHP 7.1: Callable From Closure

      No Comments on PHP 7.1: Callable From Closure

Callables are awesome. I can pass functions or object methods around  inside of variables. Sure, they can be abused to the point where you can’t recognize your code, but overall, they are awesome. Could they be made more awesome? Core Developer Dan Ackroyd seems to think so and so he proposed and got passed PHP RFC: Closure from callable function

“PHP RFC: Closure from callable function” is about making life easier for developers that use callables and Closures. If you don’t already use them, this won’t make your life any easier yet. I say yet because once you see the power in callables, you will start sprinkling them in your code like Unicorn dust.

In laying out the case for the RFC, Dan gave is three reasons why this should be added to the language.

Handling Errors in the right place

When dealing with callables, especially with functions names, it can be very easy to misspell the name. The problem is, you may create the callable in one place, and use it way over in another area of your code. You may not even be maintaining the code that creates it. If it is misspelled though, it is your code that blows baby chunks. (It’s a technical term, I swear.)


With the new fromCalable() method though, the error is discovered at the source where it can be properly handled.


Exposing Private Methods

Keith Casey once said, “Only the TSA can touch your private methods.”. I’m happy to report that as of PHP 7.1, he is wrong. (Don’t worry, he’s used to the feeling) fromCallable() will allow us to expose private methods of objects by turning them into closures. In one form or another, we have been able to do this since PHP 5.4. However, now with fromCallable() it is easier and – as discussed before – less error prone. Take a look.

Notice that even though emailValidation() is a private method of the class Validator, we can turn it into a closure and expose it to anyone who needs the functionality.


Because of the way PHP currently handles a callable, it is checked every time it is called. This is a significant overhead. Because fromCallable actually creates a Closure, this type checking dow not have to be done more than once. In the test code (one of those for-loops counting to one million…because we all do that in production code) Dan reported a 16% increase in using the Closure over a standard callable. Ok, so most of us won’t write code like that in production, it is nice to know that PHP gets a little faster with every version and that the core is constantly trying to ensure that.


Callables are awesome. With this new method of the Closure class, they are now even more awesome in PHP 7.1. The TSA however, still sucks.

About Cal Evans

Many moons ago, at the tender age of 14, Cal touched his first computer. (We're using the term "computer" loosely here, it was a TRS-80 Model 1) Since then his life has never been the same. He graduated from TRS-80s to Commodores and eventually to IBM PCs.   For the past 10 years, Cal has worked with PHP and MySQL on Linux OSX, and when necessary, Windows. He has built on a variety of projects ranging in size from simple web pages to multi-million dollar web applications. When not banging his head on his monitor, attempting a blood sacrifice to get a particular piece of code working, he enjoys building and managing development teams using his widely imitated but never patented management style of "management by wandering around". Cal is happily married to wife 1.33, the lovely and talented Kathy. Together they have 2 kids who were both bright enough not to pursue a career in IT. Cal blogs at and is the founder and host of Nomad PHP