PHP5 Tutorial – call() Magic Method
Check out latest Mobile Phones and Books only at Flipkart.com
This tutorial will guide you through the __call() Magic Method. The __call Magic method in PHP5 get called when accessing an undeclared or undefined methods of an class. With this magic method the programmer can keep track on the undeclared method which are not defined inside the class.
Syntax:
< ? function __call($data,$argument) { //$data holds the name of the undefined method getting called. //$argument holds the argument passed to the method. } ?>
Example – __call Magic Method
< ? class magicmethod { function __call($data,$argument) { echo "Error accessing undefined Method"; echo "Method Called: ".$data; echo "Argument passed to the Method: ".$argument; } } $a = new magicmethod(); echo $a->setData(); //Calling setData method ?>
Output:
Error accessing undefined Method
Method Called: setData
Argument passed to the Method: Array (Array of the Argument Passed)
Explanation for the Example:
Here i am trying to call setData method of magicmethod class.
Now in the magicmethod class setData is not defined so the php compiler excutes __call() magic method and displays error message.
Related Articles:
- PHP5 Tutorial – unset() Magic Method
- Generate Unique SessionId in PHP
- OOPS in PHP5 – Define Attributes for Class
- PHP5 Tutorial – wakeup() Magic Method
- OOPS in PHP 5 Tutorial – Exploring Inheritance
- OOPS in PHP5 – Define Methods for the Class
- OOPS in PHP 5 Tutorial – Defining Class Constants
- OOPS in PHP 5 Tutorial – Function Overriding
- Writing HTML Scrapper in PHP
- PHP5 Tutorial – clone Magic Method
__call is not worked at the time of call a undefined member function using scope resolution operation..
ex: magicmethod::setData()