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

I would like to learn about popo. I searched popo and found that it means Plain Old Php Object. But I'm not sure about the exact meaning of the Plain Old Php object. I want to know what popo is and where to use it? Thank.

+5
source share
3 answers

The plain old {Insert Language Here} Object is a simple approach that says you don't always need to use an extensive class to store data or execute logic.

In PHP, you can create a β€œsimple” object by creating an instance of stdClass.

$object = new stdClass;
$object->value = "Hello World";
echo $object->value; // Hello World

Compared to a larger object that implements the interface:

interface HelloWorldInterface {

    public function getValue();
    public function setValue($value);

}

class HelloWorld implements HelloWorldInterface {

    protected $value;

    public function getValue()
    {
        return $this-value;
    } 

    public function setValue($value) {
        $this->value = $value;
    }

}

$object = new HelloWorld;
$object->setValue("Hello World");
echo $object->getValue(); // Hello World

stdClass, , , , .

+8

. stdClass, , . stdClass .

:

  • - . .
  • (/)
  • toArray(), toJson() , /
  • , .
  • -
  • POPO. []
  • , $ this . []
  • , __toString() []

:

:

POPO is a PHP version of POJO created 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;
}

}
0
source

Source: https://habr.com/ru/post/1663973/


All Articles