PHP code example of sylvrs / libmarshal

1. Go to this page and download the library: Download sylvrs/libmarshal 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/ */

    

sylvrs / libmarshal example snippets


class User {
	use MarshalTrait;
	
	public function __construct(
            #[Field(name: "first-name")]
            public string $firstName,
            #[Field(name: "last-name")]
            public string $lastName,
            public int $age,
            public string $email,
            #[Exclude]
            public string $internalData = "..."
	) {}
}

// NOTE: This uses promoted properties to make it easier to construct.
// You can learn more about this below.

// Marshalling
$user = new User(firstName: "John", lastName: "Doe", age: 30, email: "[email protected]");
$data = $user->marshal(); // ["first-name" => "John", "last-name" => "Doe", "age" => 30, "email" => "[email protected]"]

$data["first-name"] = "Jane"; // Changing the first name
$data["email"] = "[email protected]"; // Changing the email

// Unmarshalling
$user = User::unmarshal($data); // User(firstName: "Jane", lastName: "Doe", age: 30, email: "[email protected]")