PHP5 Tutorial - Magic Methods - __call() method

This tutorial will teach you how and when to use the magic method __call().

The magic method __call() is to undeclared methods what __get() and __set() are to undeclared data member.

These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the time of development.

The magic method __call() takes two arguments. The first argument is the name of the undeclared method invoked by the program and the second is an array that contains a list of parameters passed to the undeclared array.




Look at the example below:

class Customer {
 
	public function __call($name, $args) {
		var_dump($name);
		echo "\n";
		var_dump($args);
		echo "\n";
	}
}
 
$c = new Customer();
$c->setName("Sunil","Bhatia");

Output:
string(7) “setName”

array(2) {
[0]=>
string(5) “Sunil”
[1]=>
string(6) “Bhatia”
}




In the example above, an object of the Customer class is created and an undeclared method viz. $c->setName is called. The magic method __call() is internally executed which accepts two parameters. The first parameter ‘$name’ contains the name of the method i.e. ’setName’ and the second parameter ‘$args’ contains the arguments passed to the ’setName’ method i.e ‘Sunil’ & ‘Bhatia’.

Using this method, you can provide code to handle calls to undeclared method. To disallow programs to call an undeclared method, you should raise an exception from within __call() magic method.

Look at the example below:

class Customer {
 
	public function __call($name, $args) {
		throw new Exception("Undeclared method execution not allowed",10);
	}
}
 
$c = new Customer();
$c->setName("Sunil","Bhatia");

Output:
Fatal error: Uncaught exception ‘Exception’ with message ‘Undeclared method execution not allowed’ in D:sunilbwebsiteprogsmagic_call.php:6
Stack trace:
#0 [internal function]: Customer->__call(’setName’, Array)
#1 D:sunilbwebsiteprogsmagic_call.php(11): Customer->setName(’Sunil’, ‘Bhatia’)
#2 {main}
thrown in D:sunilbwebsiteprogsmagic_call.php on line 6

In the above program, when the script calls an undeclared variable $c->setName(), the magic method __call() is executed. On executing the magic method __call(), an exception is raised and the execution of the program stops there (unless we use the try..catch statements)

Related Posts on PHP5 Tutorial - Object Oriented Programming (OOPS)

  1. PHP5 Tutorial - Learn to create a PHP5 Class
  2. PHP5 Tutorial - Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial - Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial - Defining Methods of a PHP5 Class
  5. PHP5 Tutorial - Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
  8. PHP5 Tutorial - Magic Methods - __toString() method
  9. PHP5 Tutorial - Magic Methods - __get() and __set()
  10. PHP5 Tutorial - Magic Methods - __isset() and __unset()
  11. PHP5 Tutorial - Magic Methods - __call() method
  12. PHP5 Tutorial - Magic Methods - __autoload() method
  13. PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
  14. PHP5 Tutorial - Magic Methods - __clone() method

del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Windows Live Furl Netscape Yahoo Bloglines Bookmark.it Ask Spurl Diigo

3 Responses to “PHP5 Tutorial - Magic Methods - __call() method”

  1. […] PHP5 OOPS Tutorial - Magic Methods - __call() method […]

  2. […] PHP5 OOPS Tutorial - Magic Methods - __call() method […]

  3. I think this will not used at many places, bcos i wanna process arguments in different for different function, at that time it wont good, is it true?

Leave a Reply

Top Internet blogs Programming Blogs - Blog Catalog Blog Directory TopOfBlogs Technology blogs Top Blog Topsites List