|
1 | 1 | [](https://packagist.org/packages/astral/php-serialize)
|
2 | 2 |
|
| 3 | +# Languages |
| 4 | + |
| 5 | +- [English](./docs/en/README.md) |
| 6 | +- [中文](./docs/zh/README.md) |
3 | 7 |
|
4 | 8 | # php-serialize
|
5 | 9 | An advanced PHP serialization tool leveraging attributes for flexible object-to-array and JSON conversion. Supports property aliases, type conversions, and nested object handling. Ideal for APIs, data persistence, and configuration management.
|
| 10 | + |
| 11 | + |
| 12 | +## Quick Start |
| 13 | + |
| 14 | +### Installation |
| 15 | + |
| 16 | +Install using Composer: |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require astral/serialize |
| 20 | +``` |
| 21 | + |
| 22 | +### Basic Usage |
| 23 | + |
| 24 | +```php |
| 25 | +use Astral\Serialize\Serialize; |
| 26 | + |
| 27 | +class User extends Serialize { |
| 28 | + public string $name, |
| 29 | + public int $age |
| 30 | +} |
| 31 | + |
| 32 | +// Create object from array |
| 33 | +$user = User::from([ |
| 34 | + 'name' => 'John Doe', |
| 35 | + 'age' => 30 |
| 36 | +]); |
| 37 | + |
| 38 | +// Access object properties |
| 39 | +echo $user->name; // Output: John Doe |
| 40 | +echo $user->age; // Output: 30 |
| 41 | + |
| 42 | +// Convert to array |
| 43 | +$userArray = $user->toArray(); |
| 44 | +// $userArray contents: |
| 45 | +// [ |
| 46 | +// 'name' => 'John Doe', |
| 47 | +// 'age' => 30 |
| 48 | +// ] |
| 49 | +``` |
| 50 | + |
| 51 | +#### Other Features |
| 52 | + |
| 53 | +1. **Immutability**: Read-only properties cannot be modified after construction |
| 54 | + |
| 55 | +```php |
| 56 | +use Astral\Serialize\Serialize; |
| 57 | + |
| 58 | +class User extends Serialize { |
| 59 | + public function __construct( |
| 60 | + public readonly string $name, |
| 61 | + public readonly int $age |
| 62 | + ) {} |
| 63 | +} |
| 64 | + |
| 65 | +$user = User::from([ |
| 66 | + 'name' => 'John Doe', |
| 67 | + 'age' => 30 |
| 68 | +]); |
| 69 | + |
| 70 | +try { |
| 71 | + $user->name = 'Jane Doe'; // Compile-time error: cannot modify read-only property |
| 72 | +} catch (Error $e) { |
| 73 | + echo "Read-only properties cannot be reassigned"; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +2. **Type-Safe Initialization** |
| 78 | + |
| 79 | +```php |
| 80 | +$user = User::from([ |
| 81 | + 'name' => 123, // Integer will be converted to string |
| 82 | + 'age' => '35' // String will be converted to integer |
| 83 | +]); |
| 84 | + |
| 85 | +echo $user->name; // Output: "123" |
| 86 | +echo $user->age; // Output: 35 |
| 87 | +``` |
| 88 | + |
| 89 | +3. **Constructor Initialization** |
| 90 | + |
| 91 | +```php |
| 92 | +use Astral\Serialize\Serialize; |
| 93 | + |
| 94 | +class User extends Serialize { |
| 95 | + public function __construct( |
| 96 | + public readonly string $name, |
| 97 | + public readonly int $age |
| 98 | + ) { |
| 99 | + // Can add additional validation or processing logic in the constructor |
| 100 | + if (strlen($name) < 2) { |
| 101 | + throw new \InvalidArgumentException('Name is too short'); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments