|
| 1 | +[](https://travis-ci.org/infomaniac-amf/php) |
| 2 | +[](https://scrutinizer-ci.com/g/infomaniac-amf/php/) |
| 3 | + |
1 | 4 | ## Intro
|
2 | 5 |
|
3 | 6 | **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)
|
76 | 79 | 'you' => string 'like' (length=4)
|
77 | 80 | ```
|
78 | 81 |
|
| 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 | + |
| 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 | + |
| 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 | + |
79 | 123 | ## Data Encoding (Serialization)
|
80 | 124 |
|
81 | 125 | The `AMF` spec allows for the serialization of several different data-types.
|
|
0 commit comments