1. Go to this page and download the library: Download webgraphe/phlux 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/ */
webgraphe / phlux example snippets
readonly class UserProfile
{
public function __construct(
public int $id,
public string $firstName,
public string $lastName,
public string $email,
) {}
}
enum Color: string
{
case RED = 'red';
case GREEN = 'green';
case BLUE = 'blue';
}
#[Webgraphe\Phlux\Attributes\Discriminator('type', self::MAPPING)]
abstract readonly class Shape extends Webgraphe\Phlux\Data
{
public const MAPPING = [
'square' => Square::class,
'circle' => Circle::class
];
public string $type;
public Color $color;
}
final readonly class Square extends Shape
{
public float $length;
}
final readonly class Circle extends Shape
{
public float $radius;
}
$square = Square::instantiate(length: 4);
$square->length; // 4
// uninitialized enum defaults to first case
$square->color; // Color::RED
$square->type; // 'square'
json_encode($square); // '{"type":"square","color":"red","length":4}'
$circle = Shape::from(['type' => 'circle', 'color':'green', 'radius' => 5.0]);
// class inferred from discriminator
$circle::class; // 'Circle'
$circle->color; // Color::GREEN
$circle->radius; // 5.0
final readonly class UserGroup extends Webgraphe\Phlux\Data
{
public int $id;
public int $name;
#[Webgraphe\Phlux\Attributes\ItemPrototype('members')]
public array $levelMembers;
#[Webgraphe\Phlux\Attributes\ItemType(UserProfile::class)]
private array $members;
}
final readonly class UserProfile extends Webgraphe\Phlux\Data
{
public string $firstName;
public string $lastName;
#[Webgraphe\Phlux\Attributes\Present]
public string $email;
public function __construct(
public int $id,
)
}
$user = new UserProfile(123);
$user->id; // 123
isset($user->firstName); // false
isset($user->lastName); // false
isset($user->email); // false
$user = UserProfile::lazyInstantiate(id: 123, firstName: 'John');
// Object is lazy
Data::isLazy($user); // true
// lastName will always be initialized
isset($user->lastName); // true
// email is only initialized if a value is given (present)
isset($user->email); // false
// Object is no longer lazy now that we accessed the lastName and email properties
Data::isLazy($user); // false
// Serialization omits uninitialized properties
json_encode($user); // '{"id":123,"firstName":"John",'lastName":""}
$userGroup = UserGroup::instantiate(members:[0 => [$user]]);
count($userGroup->levelMembers); // 1
count($userGroup->levelMembers[$level = 0]); // 1
$userGroup->levelMembers[$level][0] === $user; // true
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.