1. Go to this page and download the library: Download psx/schema 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/ */
declare(strict_types = 1);
class Person implements \JsonSerializable, \PSX\Record\RecordableInterface
{
protected ?string $firstName = null;
protected ?string $lastName = null;
#[Description("Age in years")]
protected ?int $age = null;
public function setFirstName(?string $firstName) : void
{
$this->firstName = $firstName;
}
public function getFirstName() : ?string
{
return $this->firstName;
}
public function setLastName(?string $lastName) : void
{
$this->lastName = $lastName;
}
public function getLastName() : ?string
{
return $this->lastName;
}
public function setAge(?int $age) : void
{
$this->age = $age;
}
public function getAge() : ?int
{
return $this->age;
}
public function toRecord() : \PSX\Record\RecordInterface
{
/** @var \PSX\Record\Record<mixed> $record */
$record = new \PSX\Record\Record();
$record->put('firstName', $this->firstName);
$record->put('lastName', $this->lastName);
$record->put('age', $this->age);
return $record;
}
public function jsonSerialize() : object
{
return (object) $this->toRecord()->getAll();
}
}
$person = new Person();
$person->setFirstName('foo');
$person->setLastName('bar');
$person->setAge(32);
echo json_encode($person);
// would result in
// {"firstName": "foo", "lastName": "bar", "age": 32}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.