What’s up buddy, today we are going to learn about getter and setter methods in the php.
Well, what if we want a string with particular length e.g. 50 chars and only alphabets to assign to a $name
property. I mean we want to validate the value before assigning it to a property.
This can be a valid reason to use the getter and setter method. Feel free to comment if you have another reason to do the same.
So, we can set the property as private or protected. So, that it would be non-accessible outside the class. Our getter and setter methods will manage these properties from the outside when needed.
So, we can create simple getter and setter methods like for a specific property. For example, we have a property called $name
and we want to set this property outside the class then we can use the setName()
method as a setter for this property. Then we can use the getName()
method as a getter to get the value of this property.
What if we have 10 properties?
Do we need 10 different getter and setter methods? NO.
One solution is, we can create common methods to deal with this situation. I mean we can create only two methods: setProp()
and getProp()
that’s it. Then we can pass the property name and new value as parameters to the method.
The method will check if the property exists in the class then it sets the value to the property. Otherwise it will return an error.
Another solution is, magic..
Magic methods like __get()
and __set()
. As we know that these magic methods can help us to deal with the non-accessible and undeclared properties, so we can use these methods here as getter and setter too.
But this time we can check if the property exists in the class, only then perform the operation otherwise just throw an error. The benefit to using magic methods is that we don’t need to call any method outside, we can call properties directly and perform action with them.
<?php namespace Test\GetterSetter; class GetSet { private $name; protected $email; public function __construct() { echo "Hello from GetSet class \n"; } // getter setter methods for all non-accessible properties public function getProp($property) { $this->showError($property); return $this->$property; } public function setProp($property, $value) { $this->showError($property); $this->$property = $value; } // getter setter methods for specific properties public function getName() { return $this->name; } public function setName($value) { $this->name = $value; } // magic methods to access non-accessible properties public function __set($property, $value) { $this->showError($property); $this->$property = $value; } public function __get($property) { $this->showError($property); return $this->$property; } // Property not found error private function showError($property) { if(!property_exists($this, $property)) { echo $property . " is undefined property \n"; die; } } } $obj = new GetSet(); //echo $obj->name; echo "Getter and setter for each property \n"; $obj->setName("harry"); echo $obj->getName() . "\n\n"; echo "Simple but common methods to deal with getter and setter \n"; $obj->setProp("name", "Rohan"); echo $obj->getProp("name") . "\n\n"; echo "Magic methods call non-accessible but existing properties : \n"; $obj->name = "Nameeta"; echo $obj->name . "\n"; echo "Magic methods call undefined properties : \n"; $obj->status = "done"; echo $obj->status . "\n\n"; /* ---------------------------------------- *//* output *//* ---------------------------------------- *//* Hello from GetSet class Getter and setter for each property harry Simple but common methods to deal with getter and setter Rohan Magic methods call non-accessible but existing properties : Nameeta Magic methods call undefined properties : status is undefined property */
Hope you enjoy the article, if so, please make sure to subscribe to the newsletter and feel free to share your suggestions via comments.
Wait, is it true that we don’t need getter and setter methods in php anymore or they are dead?
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…