Home > PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials > PHP5 OOPS Tutorial __get() and __set()

PHP5 OOPS Tutorial __get() and __set()

October 26th, 2007 admin Leave a comment Go to comments

This article talks about the use of __get() (double underscore – get()) and __set() (double underscore – set()) PHP5 OOPS magic methods.




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.

<?php

class Customer {
public $name;
}

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

$c->email = “[email protected]”; //assigning [email protected] 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:

<?php

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 = “Sunil”; // $name is set because its public

$c->email = “[email protected]”; //assigning [email protected] to the $email variable.

echo $c->email;

?>

In the above example when [email protected] 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. [email protected] is passed to $vl variable of the __set() method.




The next step is to store these values into the $data array so that you could retrieve it later.

The __get() method works in the similar fashion. When you echo $c->email, __get() method is called and the name email is passed in the $dt of the __get() method.

Tip:
It is possible to stop this behavior of PHP to assign values to undefined issues. The solution is that you raise an exception from within __set() method. Look at the code below:

<?

class Customer {

private $name;

public function __set($dt, $vl) {
throw new Exception(“Cannot assign values to undefined variables”,1);
}

}

$c = new Customer();
$c->email = “[email protected]”; //this will cause an exception to be raised

?>

Related Articles:
PHP5 OOPS – Magic Methods __toString()
PHP5 OOPS Tutorial


Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Gifford
    October 23rd, 2009 at 01:34 | #1

    name = “Sunil”; // $name is set because its public
    $c->email = [email protected]; //assigning [email protected] to the $email variable.
    ?>

    I tested php5.3 and it always say “parse error” for $c->email=”ac”; How did you test?

  2. Gifford
    October 24th, 2009 at 02:50 | #2

    I know problem is caused by ‘@’ in email

  1. November 3rd, 2007 at 13:00 | #1
  2. November 3rd, 2007 at 14:07 | #2
  3. November 8th, 2007 at 22:57 | #3
Enter this code to leave comment (Sorry, but bots get me crazy :) )