Skip to content

Commit 1b71c12

Browse files
committed
Added class-mapping guide
1 parent 80914e4 commit 1b71c12

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![Build Status](https://travis-ci.org/infomaniac-amf/php.png?branch=master)](https://travis-ci.org/infomaniac-amf/php)
2+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/infomaniac-amf/php/badges/quality-score.png?s=022a26ec698ee16f981a1d2f687574bbcc9135f4)](https://scrutinizer-ci.com/g/infomaniac-amf/php/)
3+
14
## Intro
25

36
**AMF** (Action Message Format) is a binary data serialization protocol. Simply put, it transforms objects in memory into a binary string, and can reverse this binary string into an object in memory. It can be used just like `JSON`, and this library has been build to provide a similar API to that exposed by the `JSON` functionality in `PHP`.
@@ -76,6 +79,47 @@ array (size=2)
7679
'you' => string 'like' (length=4)
7780
```
7881

82+
## Extra Features
83+
84+
### Class-mapping
85+
86+
`AMF` allows you to encode an object and retain some metadata about it; for example, when serializing an instance of a class (not `stdClass`) the library will retain the class' fully qualified namespace name and use it to reconstruct an object of that type upon decoding.
87+
88+
Consider the following class:
89+
90+
```php
91+
<?php
92+
93+
class Person {
94+
public $name;
95+
}
96+
```
97+
98+
If we encode an instance of this object, by default its class type will be ignored and when the data is decoded, the resulting value will be a plain PHP `stdClass` instance.
99+
100+
This is how the encoded data will look in Charles:
101+
102+
![](http://cl.ly/image/0p3K0I1g1Q0J/Image%202014.04.13%2012%3A42%3A56%20PM.png)
103+
104+
In order to retain the class type in `AMF`, you will need to add an additional flag to the `amf_encode` function call:
105+
106+
```php
107+
amf_encode($data, AMF_CLASS_MAPPING);
108+
```
109+
110+
When the `AMF_CLASS_MAPPING` flag is given, the encoded data will look like this in Charles:
111+
112+
![](http://cl.ly/image/32310O300I3S/Image%202014.04.13%2012%3A40%3A34%20PM.png)
113+
114+
**Notice the additional `Person` metadata in this response**
115+
116+
Now, when this data is decoded, the library will attempt to create a new instance of the `Person` class and set its public property `name` to `"bob"`.
117+
118+
```php
119+
object(Person)[8]
120+
public 'name' => string 'bob' (length=3)
121+
```
122+
79123
## Data Encoding (Serialization)
80124

81125
The `AMF` spec allows for the serialization of several different data-types.

0 commit comments

Comments
 (0)