Home > PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials > PHP5 Tutorial – Magic Methods – __call() method

PHP5 Tutorial – Magic Methods – __call() method

November 3rd, 2007 admin Leave a comment Go to comments

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
Categories: PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials Tags:

41
Comments (2) Trackbacks (2) Leave a comment Trackback
  1. spacer
    kirubakar
    December 4th, 2008 at 12:07 | #1
    Reply | Quote

    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?

  2. spacer
    Led Lighting
    November 20th, 2009 at 06:25 | #2
    Reply | Quote

    @Kirubakar -> This method is fantastic and while you will want to write your own methods this takes care of all your getters/setters

  1. November 8th, 2007 at 23:05 | #1
    PHP5 OOPS Tutorial – Defining Attributes of a PHP5 Class | Geek Files
  2. November 8th, 2007 at 23:08 | #2
    PHP5 OOPS Tutorial – Defining Methods of a PHP5 Class | Geek Files
Enter this code to leave comment (Sorry, but bots get me crazy :) )
spacer


Subscribe to comments feed
PHP5 Tutorial – Magic Methods – __autoload() method PHP5 Tutorial – Magic Methods – __isset() and __unset()
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.