PHP5 OOPS Tutorial __get() and __set()
Posted on : 26-10-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials
5
If you're new here, you may want to subscribe to my Newsletter. Thanks for visiting!
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@domain.com”; //assigning email@domain.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:
<?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@domain.com”; //assigning email@domain.com to the $email variable.
echo $c->email;
?>
In the above example when email@domain.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. email@domain.com 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@domain.com”; //this will cause an exception to be raised?>
Related Articles:
PHP5 OOPS - Magic Methods __toString()
PHP5 OOPS Tutorial









[...] my previous PHP5 OOPS tutorial on PHP5 OOPS Tutorial - Magic Methods - __get() and __set(), we learnt how and when to use these magic [...]
[...] magic method __call() is to undeclared methods what __get() and __set() are to undeclared data [...]
[...] PHP5 OOPS Tutorial - Magic Methods - __get() and __set() [...]
name = “Sunil”; // $name is set because its public
$c->email = “email@domain.com”; //assigning email@domain.com to the $email variable.
?>
I tested php5.3 and it always say “parse error” for $c->email=”ac”; How did you test?
I know problem is caused by ‘@’ in email