PHP code example of domagoj03 / object-creator

1. Go to this page and download the library: Download domagoj03/object-creator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

domagoj03 / object-creator example snippets

 
class DemoObject extends ArrayObjectCreator // or StdClassObjectCreator
{
    protected $propertyMapper = [
        'number' => 'SomeNumber',
    ];
    
    /**
     * @var int
     */
    private $number;

    /**
     * @return int
     */
    public function getNumber(): ?int
    {
        return $this->number;
    }

    /**
     * @param int $number
     * @return DemoObjectFromArray
     */
    public function setNumber(int $number): DemoObjectFromArray
    {
        $this->number = $number;

        return $this;
    }
}

// Usually as a result of an API call
$data = [
    'SomeNumber' => 23
];

$object = new DemoObject($data);
$object->getNumber(); // 23

$data = new \stdClass();
$data->number = 23;


$object = new DemoObject($data); // Has to extend StdClassObjectCreator
$object->getNumber(); // 23