Smart Class in PHP using getter & setter
Hello readers,
This article is about smart class in PHP so it will only contain business logic.
In another article Clean Code In PHP, I wrote about clean coding in PHP for beginners. But if you want to go for next level then this article is for you. Every developers must try to be smart in coding. If you have mastered some architecture, then don’t stop there thinking that you have learned smart way of coding.
Frankly speaking I’m not smart coder, but yes I always try to apply new techniques to make my code simple and smart. Yes, your code must be simple, there is myth in most of the beginners mind that the developer who writes complex code, which is not easy to understand is smart programmer. But it’s not true. I’ll write more about it in another article. Let’s focus on this article.
This article is one step to make class simple and more focused towards its business logic. Let’s take example of class file from article Clean Code In PHP.
Does above class looks smart? No it’s not smart class. The problem with above class is it’s properties are public, but the class property must be private/protected. And then access them using get/set methods for each one of the property. Like for $name we can write get/set method as (Created new class for example):
Above you can see that you can set the value of private name using setName and access it using getName routine.
Doesn’t it look simple? My answer is NO. Let’s imagine what if number of properties increases, So don’t you think we have to create two routine for each property (even if variables are different,still the working logic is same).
So,why not create one simple method which will act as getter and setter irrespective of the number of properties. For this we will use magic method __call. This method is called whenever the called routine of class is not found. For example if class don’t contain routine getName then __call is executed if getName is called by object of the class.
__call has 2 parameters :
1: Name of method
2: Arguments passed to the method
For this to work you must follow the specific naming conventions. Those are as follow.
1: First character of property name must be lowercase.
2: Get/Set method must have same property name as specified in class and case-sensitive (Not for 1st charactor).
For example:
Now let’s code for __call method. The class will be like :
Now let’s use this infrastructure to get/set property.
This __call can be used by all the classes. For this I wrap this __call routine in abstract class and then every class extends that abstract class,so no need to write same routine for each class separately.
Now,there is also one problem. What if we want to create some custom set or get method for specific property?
The solution is simple just create the routine in class, __call will be called only when the routine is not found in class,if it’s found then __call is never going to execute.
There are more ways to minimize the class, I have explained one approach of minimizing class using getter/setter.
If you have any query/suggestion please comment below. And if you like these article, then like and share it.
This article is about smart class in PHP so it will only contain business logic.
In another article Clean Code In PHP, I wrote about clean coding in PHP for beginners. But if you want to go for next level then this article is for you. Every developers must try to be smart in coding. If you have mastered some architecture, then don’t stop there thinking that you have learned smart way of coding.
Frankly speaking I’m not smart coder, but yes I always try to apply new techniques to make my code simple and smart. Yes, your code must be simple, there is myth in most of the beginners mind that the developer who writes complex code, which is not easy to understand is smart programmer. But it’s not true. I’ll write more about it in another article. Let’s focus on this article.
This article is one step to make class simple and more focused towards its business logic. Let’s take example of class file from article Clean Code In PHP.
<?php class Student { public $student_id; public $name; public $roll_no; public $marks; }
Does above class looks smart? No it’s not smart class. The problem with above class is it’s properties are public, but the class property must be private/protected. And then access them using get/set methods for each one of the property. Like for $name we can write get/set method as (Created new class for example):
<?php class Name { private $name; public function getName() { return $this->name; } public function getName($nm) { $this->name=$nm; } } ?>
Above you can see that you can set the value of private name using setName and access it using getName routine.
Doesn’t it look simple? My answer is NO. Let’s imagine what if number of properties increases, So don’t you think we have to create two routine for each property (even if variables are different,still the working logic is same).
So,why not create one simple method which will act as getter and setter irrespective of the number of properties. For this we will use magic method __call. This method is called whenever the called routine of class is not found. For example if class don’t contain routine getName then __call is executed if getName is called by object of the class.
__call has 2 parameters :
1: Name of method
2: Arguments passed to the method
For this to work you must follow the specific naming conventions. Those are as follow.
1: First character of property name must be lowercase.
2: Get/Set method must have same property name as specified in class and case-sensitive (Not for 1st charactor).
For example:
<?php //For property private $firstName; //get method is $obj->getFirstName(); //set method must be $obj->setFirstName('Mayur'); ?>
Now let’s code for __call method. The class will be like :
<?php class Student { private $studentId; private $firstName; private $rollNo; private $marks; public function __call($name, $arguments) { $type= substr($name,0,3); $property= strtolower($name[3]).substr($name,4); // $name[3] must be lower as our property names are lowercase. switch ($type) { case "get": return $this->$property; break; case "set": if (count($arguments)==1) { $this->$property=$arguments[0]; }else{ throw new Exception ("Setter method must have 1 argument"); } break; case "default": throw new Exception ("Method is not defined in the class"); } } {
Now let’s use this infrastructure to get/set property.
<?php require_once './Student.class.php'; $studObj=new Student(); $studObj->setFirstName('Mayur'); echo $studObj->getFirstName(); //prints Mayur $studObj->setFirstName('Mayur','Deore'); echo $studObj->getFirstName(); //throws exception setter method must have 1 argument ?>
This __call can be used by all the classes. For this I wrap this __call routine in abstract class and then every class extends that abstract class,so no need to write same routine for each class separately.
Now,there is also one problem. What if we want to create some custom set or get method for specific property?
The solution is simple just create the routine in class, __call will be called only when the routine is not found in class,if it’s found then __call is never going to execute.
There are more ways to minimize the class, I have explained one approach of minimizing class using getter/setter.
If you have any query/suggestion please comment below. And if you like these article, then like and share it.
Comments
Post a Comment