1
- ## 快速开始
1
+ ## Quick Start
2
2
3
- ### 安装
3
+ ### Installation
4
4
5
- 使用 Composer 安装:
5
+ Install using Composer:
6
6
7
7
``` bash
8
- composer require astral/serialize
8
+ composer require astral/php- serialize
9
9
```
10
10
11
- ### 基本用法
11
+ ### Basic Usage
12
12
13
13
``` php
14
14
use Astral\Serialize\Serialize;
@@ -18,28 +18,28 @@ class User extends Serialize {
18
18
public int $age
19
19
}
20
20
21
- // 从数组创建对象
21
+ // Create object from array
22
22
$user = User::from([
23
- 'name' => '张三 ',
23
+ 'name' => 'John Doe ',
24
24
'age' => 30
25
25
]);
26
26
27
- // 访问对象属性
28
- echo $user->name; // 输出: 张三
29
- echo $user->age; // 输出 : 30
27
+ // Access object properties
28
+ echo $user->name; // Output: John Doe
29
+ echo $user->age; // Output : 30
30
30
31
- // 转换为数组
31
+ // Convert to array
32
32
$userArray = $user->toArray();
33
- // $userArray 的内容 :
33
+ // $userArray contents :
34
34
// [
35
- // 'name' => '张三 ',
35
+ // 'name' => 'John Doe ',
36
36
// 'age' => 30
37
37
// ]
38
38
```
39
39
40
- ### 其他特性
40
+ #### Other Features
41
41
42
- #### ** 不可变性 ** :只读属性在构造后无法修改
42
+ 1 . ** Immutability ** : Read-only properties cannot be modified after construction
43
43
44
44
``` php
45
45
use Astral\Serialize\Serialize;
@@ -52,30 +52,30 @@ class User extends Serialize {
52
52
}
53
53
54
54
$user = User::from([
55
- 'name' => '张三 ',
55
+ 'name' => 'John Doe ',
56
56
'age' => 30
57
57
]);
58
58
59
59
try {
60
- $user->name = '李四 '; // 编译时错误:无法修改只读属性
60
+ $user->name = 'Jane Doe '; // Compile-time error: cannot modify read-only property
61
61
} catch (Error $e) {
62
- echo "只读属性不能被重新赋值 ";
62
+ echo "Read-only properties cannot be reassigned ";
63
63
}
64
64
```
65
65
66
- #### ** 类型安全的初始化 **
66
+ 2 . ** Type-Safe Initialization **
67
67
68
68
``` php
69
69
$user = User::from([
70
- 'name' => 123, // 整数会被转换为字符串
71
- 'age' => '35' // 字符串会被转换为整数
70
+ 'name' => 123, // Integer will be converted to string
71
+ 'age' => '35' // String will be converted to integer
72
72
]);
73
73
74
- echo $user->name; // 输出 : "123"
75
- echo $user->age; // 输出 : 35
74
+ echo $user->name; // Output : "123"
75
+ echo $user->age; // Output : 35
76
76
```
77
77
78
- #### ** 构造函数初始化 **
78
+ 3 . ** Constructor Initialization **
79
79
80
80
``` php
81
81
use Astral\Serialize\Serialize;
@@ -85,9 +85,9 @@ class User extends Serialize {
85
85
public readonly string $name,
86
86
public readonly int $age
87
87
) {
88
- // 可以在构造函数中添加额外的验证或处理逻辑
88
+ // Can add additional validation or processing logic in the constructor
89
89
if (strlen($name) < 2) {
90
- throw new \InvalidArgumentException('名称太短 ');
90
+ throw new \InvalidArgumentException('Name is too short ');
91
91
}
92
92
}
93
93
}
0 commit comments