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 trying 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).
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, because it’s free.