Share on Google+Share on Google+

PHP Invoke Method

Advertisement
In this tutorial we will study about Invoke method of PHP, it is completely new kind of method and because of it's magical property it has been included in magical property of PHP. Examples in this tutorial will make it more clear.

__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)
string(5) "Hello"
float(4.5)
is_callable() method is used to check whether an object can be called or not
bool(true)

Advertisements

Posted on: March 31, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.