__get() and __set() in Php

This article talks about the use of __get() (double underscore – get()) and __set() (double underscore – set()) PHP5 magic methods.
By default PHP is a Loosely typed language and so 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 = “Hiren”; // $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:
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 = “Hiren”; // $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 Post:
PHP5 OOPS – Magic Methods __toString()

7 comments:

Sheela said...

Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

ragul ragul said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
Best Devops Training in pune
Data science training in Bangalore

tech news said...

I seriously love your site.. Excellent colors & theme. Did you develop this web site yourself? Please reply back as I’m looking to create my own blog and want to know where you got this from or just what the website theme is called. Thank you!

Anebellyliza said...

Thanks for sharing such a valuable information. The context has been explained really well. Follow us for more Info on Data Science at Data Science Training In Hyderabad

Anebellyliza said...

Hi, Thanks for sharing nice articles...


Data Science Training In Hyderabad

nikhil reddy said...

Hi, Thanks for sharing nice stuff...

Data Science Training In Hyderabad

VMS Degree College said...


The Content You Shared With Us Is Excellent & Extraordinary Useful to all Aspirants Thanks For Sharing With Us!
Best Degree College In Hyderabad
Top Degree College In Hyderabad
Top And Best BBA College In Hyderabad
Top And Best B.Com College In Hyderabad