PHP code example of smoothphp / serialization

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

    

smoothphp / serialization example snippets



namespace App;
use SmoothPhp\Contracts\Serialization\Serializable;

class MyDTO implements Serializable {
	public function __construct(Id $id, DateTime $creationDate) {
		$this->id = $id;
		$this-creationDate = $creationDate;
	}

	// getters

	public function serialize()
	{
		return [
			'id'   => (string) $this->id,
			'date' => $this->creationDate->serialize(),
		];
	}

	public static function deserialize(array $data)
	{
		return new static(
			new Id($data['id']),
			DateTime::deserialize($data['date'])
		);
	}
}

$dto = new App\MyDTO(new App\Id(uuid()), App\DateTime::now());

$serializer = new \SmoothPhp\Serialization\ObjectSelfSerializer;

// Store in database, job queue, etc
$serialized = $serializer->serialize($dto);

// Use in app
$object = $serializer->deserialize($serialized);