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)
- PHP5 Tutorial – Learn to create a PHP5 Class
- PHP5 Tutorial – Learn to Create a PHP5 Class Object
- PHP5 Tutorial – Defining Attributes of a PHP5 Class
- PHP5 Tutorial – Defining Methods of a PHP5 Class
- PHP5 Tutorial – Creating a PHP5 Constructor __construct()
- PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
- PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
- PHP5 Tutorial – Magic Methods – __toString() method
- PHP5 Tutorial – Magic Methods – __get() and __set()
- PHP5 Tutorial – Magic Methods – __isset() and __unset()
- PHP5 Tutorial – Magic Methods – __call() method
- PHP5 Tutorial – Magic Methods – __autoload() method
- PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
- PHP5 Tutorial – Magic Methods – __clone() method
41
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?
@Kirubakar -> This method is fantastic and while you will want to write your own methods this takes care of all your getters/setters