Custom Search

PHP 5 Tutorial - __call() Magic Method

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.

Custom Search

Related Post

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)