I would like to know about popo. I have searched popo and found it stands for Plain Old Php Object. But I am not sure the exact meaning of Plain Old Php Object. I want to know what is popo and where to use it? Thanks.
-
Where have you seen it used?– Mark BakerDec 16, 2016 at 15:55
-
same concept.– mister martinDec 16, 2016 at 15:55
-
Hello mister, I want to know what is the POPO same with?– Nomura NoriDec 16, 2016 at 15:56
-
Did you read it? It references POPO. Same "concept."– mister martinDec 16, 2016 at 15:59
-
Then Kindly check here. laravel.com/docs/5.3/releases and search "popo".– Nomura NoriDec 16, 2016 at 16:00
3 Answers
Plain Old {Insert Language Here} Object is an simple approach that says you don't always need to use an extensive class, or inheritance chain, to store data or perform logic. It allows you to simplify the understanding of your structure by encapsulating details, not deriving details or creating dependencies on other sources.
A simple use case could be a DTO or Entity that encapsulates user fields:
class User {
public $firstName;
public $lastName;
}
Versus a more extensive object that implements an interface or extends a Base class or both.
interface UserInterface {
public function getName();
public function setName($name);
}
class User extends Model implements UserInterface {
public function getName()
{
...
}
public function setName($value) {
...
}
}
According to Wikipedia, a named class is considered a plain object if it does not implement any interfaces or extend any preexisting classes.
I recommend looking at Shaunak Sontakke's answer for use cases.
-
1Your
versus
example has a misleading fact.protected
members of a class cannot be accessed outside. You could make it topublic
to avoid misleading even though it's just an example to make the OP understand the concept. Dec 16, 2016 at 16:04 -
1@Perumal93 thanks, fixed. Just typed protected out of habit I suppose.– DevonDec 16, 2016 at 16:05
-
1
stdClass
is also the default class. JSON decodes to it and casting to(object)
casts to it. Dec 16, 2016 at 17:22 -
-
1The idea of a Plain old object is about removing a lot of the complexity (ie ORM, DTO, DAO). The idea really came from POJO (Java) because they like acronyms and giving that lead to more use of these less complex clean class. Calling it with
stdClass
is not completely needed if you wanted the contract. Mar 18, 2019 at 17:16
POPO is as an object of a simple class. When you make it as a stdClass object it's not a strict contract of what you would have in that object. stdClass object can be easily abused by other developers.
Consider below points while making a POPO:
- All member variables are private. Accessor methods should be used vs direct member access.
- Public accessor methods ( getters / setters )
- Public toArray(), toJson() like methods for easy access, easy storage/persistence
- Constructor accepting array of values that need to be set.
- It should not have any business logic
- Avoid any kind of validation in this object
- Don't worry about implementing an interface for POPO. [Optional]
- If you need method chaining, return $this from your setters. [Optional]
- Probably a __toString() method [Optional]
When to use it:
Consider use of POPO in these cases:
- To transfer data across the boundary. Like transferring data from Model to Controllers or from controllers to View or from one application to other application. In this case it would behave like Data Transfer Object (DTO). More details - DTO vs Value Object vs POCO
- If we have a json data, we could bind that json data and map it to a POPO. So we exactly know what's there in json field. This will also ensure that our json data has a consistent structure always. It will also prevent abuse of JSON structure.
- If there are too many parameters for a function, we could use "Introduce Parameter Object" refactoring to replace all those parameters with a POPO containing those members.
- For mapping a database composite type
POPO is a PHP variant of POJO that was coined by Martin Fowler
eg:
class CPerson {
private $m_strNameFirst;
private $m_strNameLast;
private $m_fltHeight;
private $m_fltWeight;
public function __construct( $arrmixValues ) {
$this->setNameFirst( $arrmixValues['name_first'] ?? NULL );
$this->setNameLast( $arrmixValues['name_last'] ?? NULL );
$this->setHeight( $arrmixValues['height'] ?? NULL );
$this->setWeight( $arrmixValues['weight'] ?? NULL );
}
public function toArray() : array {
return [
'name_first' => $this->getNameFirst(),
'name_last' => $this->getNameLast(),
'height' => $this->getHeight(),
'weight' => $this->getWeight(),
];
}
public function getNameFirst() {
return $this->m_strNameFirst;
}
public function setNameFirst( $strNameFirst ) {
$this->m_strNameFirst = $strNameFirst;
}
public function getNameLast() {
return $this->m_strNameLast;
}
public function setNameLast( $strNameLast ) {
$this->m_strNameLast = $strNameLast;
}
public function getHeight() {
return $this->m_fltHeight;
}
public function setHeight( $fltHeight ) {
$this->m_fltHeight = $fltHeight;
}
public function getWeight() {
return $this->m_fltWeight;
}
public function setWeight( $fltWeight ) {
$this->m_fltWeight = $fltWeight;
}
}
-
POJO and getter/setters are very Java specific. There are lot of libraries that rely on the getter/setter for reflection. So in Java that is an expectation. I would not enforce something like that on PHP Mar 18, 2019 at 17:22
-
@nerdlyist So are you suggesting to skip getter / setters and make all member variables as public, so they can be directly set? Apr 13, 2019 at 7:01
-
1Make the attributes that are logically public public and the ones that are only supposed to be used by the class or system as private. In php that would be a plain old object. Now when you use something like an orm it becomes a very different type of object and you have to follow that api contract. Apr 14, 2019 at 12:34
-
Underrated answer, I wish more answers would include examples and use cases. Nov 23, 2020 at 23:58
Its from POJO
In software engineering, a plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction and not requiring any class path.
http://www.javaleaks.org/open-source/php/plain-old-php-object.html