loading...
Cover image for Plain old PHP Object - Usability and benefits in modern PHP programming

Plain old PHP Object - Usability and benefits in modern PHP programming

fuadardiono profile image Muhammad Fuad Ardiono ・3 min read

If you familiar with Java, maybe you already known about POJO (Plain Old Java Object). Because the term of POJO first coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie on Java programming language in 2000. POJO gives us ease in understanding the types of data structures that are in a class object.

We have the case:

  1. So what is the name of POJO in another programming language?
  2. Why POPO in modern PHP programming?
  3. How to make POPO in modern php programming?
  4. How to implement POPO in modern PHP programming?

Answer the case:

  1. So what is the name of POJO in another programming language?
    The convention about Plain Old Object is "Plain Old programming_language_name Object". So if you on Ruby, you can call PORO (Plain Old Ruby Object) maybe Rubyist already known about it. If on PHP it's called POPO (Plain Old PHP Object). Every language can implement Plain Old Object if they have a class.

  2. Why POPO in modern PHP programming?
    POPO can guide us about data structure, and give us clarity about what the type data on that object. You can create DTO (Data Transfer Object) inside of POPO, example guide: "toArray()", "castToClass()", "toJSON()" something like that. So you carries the data or value not in variable, but on class object based. It can gives you standardisation about how to communicate from object A to object B. And give you less function paramater, you just pass with POPO class. In modern PHP programming web application we often use MVC (Model, View, Controller) then it will have often communication each object. From controller to service, controller get data return from service, and return to view. With POPO we can make better communication each object.

  3. How to make POPO in PHP programming?
    You can make POPO by define what the class?, what the property? and then you can make it. POPO should have setters and getters. First things we define the class, Food have property name, price, quantity, and total price. Then we can set the value by setter function on Food POPO. If we want transform it to JSON we can call "toJSON()" function and etc. You can develop according to what you need. Example guide:
    Popo image

  4. How to implement POPO in modern PHP programming?
    You can start it by creating new POPO. In above example, we can call with php syntax "new Food()".

Example 1 (Simple Controller):
Example 1

In example 1 we use toArray() to communicate to Model.

Example 2 (Controller, Service and Model):
Example 2 controller

We pass class object to service
Example 2 service

We use toArray() to communicate to Model.

On below with no POPO our function store on food service is looked not maintainable, too much parameter.
Bad code

Conclusion

That is a little quick implementation POPO in modern PHP. You can use POPO for simplify your parameter function and you can get better communication each object or class. You can explore more and implement to your own development according what you need. Any question, feel free to comment on below. Happy code!!

Reference

  1. Plain old Java Object
  2. Data Transfer Object

Posted on Apr 21 by:

fuadardiono profile

Muhammad Fuad Ardiono

@fuadardiono

22 years old young man, love reading books and research something.

Discussion

markdown guide