Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial - Magic Methods - __call() method

Posted on : 03-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials

4

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

Comments (4)

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

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

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

Write a comment

Enter this code