Since the release of PHP 5.3, the magical method __invoke() is available to be used within classes. But what does this really mean? Since this new magical method is now available, we are able to invoke instances of a class as a function similar to the available () operator overloading in other languages such as C++. I would imagine that this concept is new to many of PHP programmers if they have not had the joys of programming in languages with operator overloading.
Here is a basic example of a class implementing the __invoke() method in PHP:
class MyClass
{
public function __invoke()
{
return 'Hello World';
}
}
$obj = new MyClass();
echo $obj(); // Resulting in "Hello World" being printed
Here is a similar example using operator overloads in C++.
class MyClass
{
public:
std::string operator()()
{
return "Hello World";
}
};
int main( void )
{
MyClass obj = MyClass();
std::cout < < obj(); // Resulting in "Hello World" being printed
}
As you can see - this new magical method can now provide us with some create functionality leaving us writing less code. I hope this little tip will help you in creating your next create web application with PHP. Happy coding!

What use is there for this new magic method though? Why would you call an object as a function?
Hi Sean,
In my personal opinion and from experience there is almost absolutely no use for this “magical method”. Basically when an object implements this method, the object instance is now callable. Some PHP functions have callbacks, but unfortunately at the moment I can not come up with and or find a practical use for the implementation of this method that can not easily be done other ways in PHP that are a bit more practical in my opinion.
However, in languages such as C++, functors are very similar to function pointers. Therefore, if a function requires a pointer to a function an object instance can be passed instead acting as a function pointer.
Anyways, I simply wrote this post letting people know that this capability is now available in PHP 5.3. Once I do come across a practical use for this in PHP, I will be sure to comment on it and let you know.
Maybe functors can benefit from this?
See here:
http://thereisamoduleforthat.com/content/functors-your-single-parameter-callback-blues
As with most other additions like this, usually… no, actually it is _always_ possible to accomplish the same thing another way. The benefits aren’t that these features enable us to create programs and algorithms that we couldn’t have made before. Rather, the benefits are they they enable to to do so in a much more flexible and comfortable way.
Functors are useful because they allow classes and functions to become interchangeable. Implementations of the delegate design pattern would definitely benefit from functors. Sometimes you want to use anonymous functions because they are lightweight and simple to implement. Other times you need the powerful feature set of objects and classes. With functors, the implementation of the delegate pattern doesn’t need to know wether or not the delegate is a function or a class.
Basically, functors allow us to add one more layer of abstraction. And if you think about it, that’s pretty much what every other advancement in programming languages is trying to do as well.
Would this be a good use? http://github.com/SeanJA/MarkdownPHP/blob/master/classes/markdown.php
Is there something specific about the MarkDown class you were referring to Sean?
I’m using this method for implementing event system all over my php framework.
If people had no use for Functors it would not become one of the well-known design patterns…
More about it: http://en.wikipedia.org/wiki/Function_object
The usefulness is debatable in languages that DO HAVE first-class function objects, but languages that do not have first-class function objects need some sort of Functor.
Related design patterns: Command, Callback