Skip to main content

PHP5 Tutorial – 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(“Stuart”,”Broad”);
Output:
string(7) “setName”

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

 

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 ‘Stuart’ & ‘Broad’.

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(“Stuart”,”Broad”);

 

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(’Stuart’, ‘Broad’)
#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)

 

 

Read More

PHP5 Tutorial – Magic Methods – __isset() and __unset()

These methods are automatically called internally when isset() and unset() is called on undeclared data members. The magic method __isset() method receives an argument – the value of which is the name of the variable that the program wants to test if the variable is set or not.

The magic method __unset() method receives an argument – the value of which is the name of the variable that the program wants to unset.

Look at the example below:

class Customer {
private $data = array(); 

public function __set($dt, $vl) {
$this->data[$dt] = $vl;
}

public function __get($dt) {
return $this->data[$dt];
}

public function __isset($dt) {
return isset($this->data[$dt]);
}

public function __unset($dt) {
return unset($this->data[dt]);
}
}

$c = new Customer();
$c->name = “Stuart Broad”;

echo isset($c->name);
echo unset($c->name);

 

In the example above the script creates a new Customer Object. The program assigns a string value to an undeclared variable i.e. $c->name.

The program ties to check if the undeclared variable i.e., $c->name has been set or not using the PHP method isset(). Since $c->name is an undeclared variable the PHP5 magic method __isset() is invoked that takes the name of the undeclared variable and checks if the internal array $data[‘name’] is set or not.

Similarly, when the program calls unset() on the undeclared variable i.e. $c->name, the PHP5 magic method __unset() is invoked that takes the name of the undeclared variable i.e. $c->name and unsets the internal array $data[“name”].

 

 

Read More

PHP5 Tutorial – get()) and __set() magic method

By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before using them. This also holds true for using class members. Look at an example below.

class Customer {
public $name;
}$c = new Customer();
$c->name = “Stuart”; // $name is set because its public 

$c->email = “test@test.com”; //assigning test@test.com to the $email variable.

 

Ideally in a strict language this would have been an error. But, with PHP this works perfectly well as you can assign values to an undefined variable.

Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.

__set() allows you to provide functionality to validate data being stored. See example below:

class Customer {
public $name;
private $data = array(); 

public function __set($dt, $vl) {
$this->data[$dt] = $vl;
}

public function __get($dt) {
return $this->data[$dt];
}
}

$c = new Customer();
$c->name = “Stuart”; // $name is set because its public

$c->email = “test@test.com”; //assigning email@domain.com to the $email variable.

echo $c->email;

In the above example when test@test.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. test@test.com is passed to $vl variable of the __set() method.

 

 

Read More

PHP5 Tutorial – __toString() method

The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation.

Following is the example of the __toString() method:

class Customer {
private $firstName, $lastName, $email;

public function __construct($firstName, $lastName, $email) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
}

public function __toString() {
return “Debug message from Customer Class : First Name = ” . $this->firstName . “, Last Name = ” . $this->lastName . “, Email = ” . $this->email;
}
}

$c = new Customer(“Stuart”,”Broad”,”test@test.com”);

echo “Customer Object is >>” . $c;

 

Output:

Customer Object is >> Debug message from Customer Class : First Name = Stuart, Last Name = Broad, Email = test@test.com
See how in this example $c Customer Object got converted into a string type when used with the dot (.) concatenation operator. In the background the magic method __toString() is automatically called when such a conversion happens.

 

 

Read More

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!