PHP code example of programmis / auto-fill-object
1. Go to this page and download the library: Download programmis/auto-fill-object 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/ */
programmis / auto-fill-object example snippets
class Dummy
{
use lib\AutoFillObject;
private $i;
private $str;
private $dummy;
public function getDummy()
{
return $this->dummy;
}
public function setI($i)
{
$this->i = $i == 2 ? 0 : $i;
}
public function getI()
{
return $this->i;
}
public function getStr()
{
return $this->str;
}
public function objectFields()
{
return [
'dummy' => 'Dummy',
];
}
}
$json = json_encode([
'i' => 1,
'str' => 'dummy_text',
'dummy' => [
'i' => 2,
'str' => 'dummy_text_2'
]
]);
$dummy = new Dummy();
$dummy->fillByJson($json);
echo $dummy->getI() . "\n"; // 1
echo $dummy->getStr() . "\n"; // dummy_text
echo $dummy->getDummy()->getI() . "\n"; // 0 see in dummy setter
echo $dummy->getDummy()->getStr() . "\n"; // dummy_text_2