Home Tutorial Php Phpoop PHP invoke Method

 
 

PHP invoke Method
Posted on: March 4, 2010 at 12:00 AM
Advertisement
Have you ever wondered that how to call an object as function? Thanks to __invoke method, introduced in PHP 5.3.0, with the help of this function we can do it. The present tutorial is all about this PHP invoke function.

__invoke() Function in PHP:

PHP 5.3.0 has introduced so many new concepts, new functions. __invoke() is one of them. The functionality of __invoke() method is quite different from others. It is new and elegant in this class. Have you ever heard that we can call an object as function? I suppose no, but with the help of __invoke() method it is possible and just because of it's magical property it is included in magic methods.

So, let's try to evaluate what is it actually and how it works? Hope the following example will exemplify:

PHP Invoke Method 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)
Advertisement

Related Tags for PHP invoke Method:


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.

Posted on: March 4, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com