3 There are a handful of data types treated in a specific way in PHP; consider the following examples:
7 Symbols are much like strings, in that they are Unicode character sequences. The primary difference is the intended semantics: symbols represent semantic identifiers as opposed to textual literal values. Symbols are case sensitive.
9 In the text format, symbols are delimited by single-quotes and use the same [escape characters](https://amzn.github.io/ion-docs/docs/spec.html#escapes).
11 See [Ion Symbols](https://amzn.github.io/ion-docs/docs/symbols.html) for more details about symbol representations and symbol tables, and our section on [Symbols, Tables and Catalogs](ion/:%20Tutorial/:5.%20Symbols,%20Tables%20and%20Catalogs) for a distilled read.
15 See the [section on reals](ion/:%20Tutorial/:3.%20Standard%20Datatypes#Reals) for an introduction.
19 $d = new ion\Decimal(123);
20 echo ion\serialize($d), " = ", $d->isInt() ? "int" : "noint", "\n";
23 $d = new ion\Decimal("123.123");
24 echo ion\serialize($d), " = ", $d->isInt() ? "int" : "noint" ,"\n";
30 See the [official ION spec about real numbers](https://amzn.github.io/ion-docs/docs/spec.html#real-numbers) and also [Ion Float](https://amzn.github.io/ion-docs/docs/float.html) and [Ion Decimals](https://amzn.github.io/ion-docs/docs/decimal.html) for more notes.
36 The `blob` type allows embedding of arbitrary raw binary data. Ion treats such data as a single (though often very large) value. It does no processing of such data other than passing it through intact.
38 In the text format, `blob` values are denoted as [RFC 4648](https://tools.ietf.org/html/rfc4648)-compliant [Base64](http://en.wikipedia.org/wiki/Base64) text within two pairs of curly braces.
41 {{ dHdvIHBhZGRpbmcgY2hhcmFjdGVycw== }}
46 The `clob` type is similar to `blob` in that it holds uninterpreted binary data. The difference is that the content is expected to be text, so we use a text notation that’s more readable than Base64.
49 {{ "This is a CLOB of text." }}
52 See the official ION specification on [Blobs](https://amzn.github.io/ion-docs/docs/spec.html#blob) and [Clobs](https://amzn.github.io/ion-docs/docs/spec.html#clob).
56 Timestamps represent a specific moment in time, always include a local offset, and are capable of arbitrary precision.
58 Instances of ion\Timestamp are really just plain \DateTime objects augmented with Stringable and ION specific formatting.
63 precision: ion\Timestamp\Precision::FracTZ,
66 // 2022-02-25T16:11:54.118+00:00
74 precision: ion\Timestamp\Precision::Day
85 precision: ion\Timestamp\Precision::MinTZ,
86 format: ion\Timestamp\Format::Min,
87 datetime: "2020-03-15T12:34",
88 timezone: new DateTimeZone("Europe/Vienna")
91 // 2020-03-15T12:34+01:00
96 See also the [official ION Timestamp specification](https://amzn.github.io/ion-docs/docs/spec.html#timestamp).
98 ## Special PHP Objects
100 ### Deprecated Serializable:
103 > The interface Serializable has been deprecated in 8.1 and should be replaced with magic serialize methods.
108 class srlzbl implements \Serializable {
109 private $data = "foo";
110 public function serialize() {
113 public function unserialize($data) {
118 $srlzbl = new srlzbl;
121 $srlzd = ion\serialize($srlzbl);
125 object(srlzbl)#4 (1) {
126 ["data":"srlzbl":private]=>
137 Everything as expected so far, Serializable return a string, but since they cannot indicate whether it's a valid UTF-8 string, a ion\Type::CLob or ion\Type::BLob, CLobs are assumed.
139 Unserialization does not offer any surprises, either:
144 var_dump(ion\unserialize($srlzd));
147 object(srlzbl)#4 (1) {
148 ["data":"srlzbl":private]=>
157 ### Magic __serialize:
159 Implementing serialization behavior with magic methods is the preferred way since 8.1:
165 private string $foo = "foo";
166 function __serialize() : array {
167 return ["foo" => "bar"];
169 function __unserialize(array $data) : void {
170 foreach ($data as $k => $v)
178 $srlzd = ion\serialize($magic);
182 object(magic)#6 (1) {
183 ["foo":"magic":private]=>
187 O::magic::{foo:"bar"}
194 Again, unserialization yields the expected results:
199 var_dump(ion\unserialize($srlzd));
202 object(magic)#7 (1) {
203 ["foo":"magic":private]=>
212 ### Custom serialize:
214 Customly serializable objects work like magic serializable objects, with custom names for the magic methods.
221 function init(array $data) : void {
224 function export() : array {
229 $custom = new custom;
230 $custom->init(["foo" => "bar"]);
231 echo $srlzd = ion\serialize($custom);
234 c::custom::{data:p::custom::{foo:"bar"}}
241 The above is actually the result of serializing a standard class backed PHP object, because we didn't implement any serialization primitives and did neither specify a custom method to call. So let's just do that:
246 $srlzr = new ion\Serializer\PHP(callCustomSerialize: "export");
247 echo $srlzd = ion\serialize($custom, $srlzr);
250 C::custom::{foo:"bar"}
257 Note how this output compares to the output of the standard magic serializable object.
259 Unserialization works as used to, except sepcifying thwe custom unserialization method to call:
264 $unsrlzr = new ion\Unserializer\PHP(callCustomUnserialize: "init");
265 var_dump(ion\unserialize($srlzd, $unsrlzr));
268 object(custom)#10 (1) {
269 ["data":"custom":private]=>
283 An S-expression (or [symbolic expression](https://en.wikipedia.org/wiki/S-expression)) is much like a list in that it’s an ordered collection of values. However, the notation aligns with Lisp syntax to connote use of application semantics like function calls or programming-language statements. As such, correct interpretation requires a higher-level context other than the raw Ion parser and data model.
285 In the text format, S-expressions are bounded by parentheses. S-expressions also allow unquoted operator symbols (in addition to the unquoted identifier symbols allowed everywhere), so commas are interpreted as values rather than element separators.
288 null.sexp // A null S-expression value
289 () // An empty expression value
290 (cons 1 2) // S-expression of three values
291 ([hello][there]) // S-expression containing two lists
293 (a+-b) ( 'a' '+-' 'b' ) // Equivalent; three symbols
294 (a.b;) ( 'a' '.' 'b' ';') // Equivalent; four symbols
297 Although Ion S-expressions use a syntax similar to Lisp expressions, Ion does not define their interpretation or any semantics at all, beyond the pure sequence-of-values data model indicated above.
303 * [Symbols, Tables and Catalogs](ion/:%20Tutorial/:5.%20Symbols,%20Tables%20and%20Catalogs)