__invoke():
PHP 5.3.0 has introduced lots of new methods, __invoke() is one of them. The purpose of __invoke() method is different from others. With the help of this function we can call an object as functions and because of it's magical property it has been included in magic methods list of PHP.
So, let's try to evaluate what is it actually and how it works? Hope the following example will exemplify:
Example:
<?php
class A
{
public function __invoke($var){
var_dump($var);
echo"<br/>";
}
}
$obj=new A;
$obj(4);
$obj("Hello");
$obj(4.5);
echo "<b>is_callable() method is used to check whether an object can be called or not </b><br/>";
var_dump(is_callable($obj));
?>
Output:
int(4)If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.