What does the term Plain Old PHP Object (POPO) exactly mean?

后端 未结 3 1852
伪装坚强ぢ
伪装坚强ぢ 2021-02-20 01:43

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 w

3条回答
  •  花落未央
    2021-02-20 02:14

    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.

提交回复
热议问题