Datatypes

Standard datatypes

ION supports many of PHP's data types:

NULL

Additonally to the plain and simple NULL, ION can attach a type to NULL values.


<?php
  
$writer 
= new ion\Writer\Stream\Writer(STDOUT);
$writer->writeNull();
$writer->writeTypedNull(ion\Type::Int);
$writer->writeTypedNull(ion\Type::String);
$writer->flush();

/*
    null null.int null.string
*/

?>

Special datatypes

There are a handful of data types treated in a specific way in PHP; consider the following examples:

Deprecated Serializable

NOTE:
The interface Serializable has been deprecated in 8.1 and should be replaced with magic serialize methods.


<?php
  
class srlzbl implements \Serializable {
  private 
$data "foo";
  public function 
serialize() { 
    return 
"bar"
  }
  public function 
unserialize($data) { 
    
$this->data $data
  }
}

$srlzbl = new srlzbl;
var_dump($srlzbl);

$srlzd ion\serialize($srlzbl);
echo 
$srlzd;

/*
    object(srlzbl)#4 (1) {
        ["data":"srlzbl":private]=>
        string(3) "foo"
    }
    
    S::srlzbl::{{"bar"}}

*/

?>

Everything as expected so far, Serializable return a string, but since it cannot indicate whether it's a valid UTF-8 string, a ion\Type::CLob or ion\Type::BLob, CLobs are assumed.

Unserialization does not offer any surprises, either:


<?php 
  
var_dump
(ion\unserialize($srlzd));

/*
  object(srlzbl)#4 (1) {
    ["data":"srlzbl":private]=>
    string(3) "bar"
  }

*/

?>

Magic __serialize

Implementing serialization behavior with magic methods is the preferred way since 8.1:


<?php
  
class magic {
  private 
string $foo "foo";
  function 
__serialize() : array {
    return [
"foo" => "bar"];
  }
  function 
__unserialize(array $data) : void {
    foreach (
$data as $k => $v
      
$this->$k $v;
  }
}

$magic = new magic;
var_dump($magic);

$srlzd ion\serialize($magic);
echo 
$srlzd;

/*
  object(magic)#6 (1) {
    ["foo":"magic":private]=>
    string(3) "foo"
  }

  O::magic::{foo:"bar"}

*/

?>

Again, unserialization yields the expected results:


<?php
  
var_dump
(ion\unserialize($srlzd));

/*
  object(magic)#7 (1) {
    ["foo":"magic":private]=>
    string(3) "bar"
  }

*/

?>

Custom serialize

Customly serializable objects work like magic serializable objects, with custom names for the magic methods.


<?php
  
class custom {
  private array 
$data;
  function 
init(array $data) : void {
    
$this->data $data;
  }
  function 
export() : array {
    return 
$this->data;
  }
}

$custom = new custom;
$custom->init(["foo" => "bar"]);
echo 
$srlzd ion\serialize($custom);

/*
    c::custom::{data:p::custom::{foo:"bar"}}
    
*/

?>

The above is actually the result of serializing a plain old 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:


<?php
  
$srlzr 
= new ion\Serializer\PHP(callCustomSerialize"export");
echo 
$srlzd ion\serialize($custom$srlzr);

/*
    C::custom::{foo:"bar"}

*/

?>

Note how this output compares to the output of the standard magic serializable object.

Unserialization works as used to, except sepcifying thwe custom unserialization method to call:


<?php
  
$unsrlzr 
= new ion\Unserializer\PHP(callCustomUnserialize"init");
var_dump(ion\unserialize($srlzd$unsrlzr));

/*
    object(custom)#10 (1) {
    ["data":"custom":private]=>
    array(1) {
      ["foo"]=>
      string(3) "bar"
    }
  }

*/

?>

Decimal

LOB

Symbol

Timestamp

Instances of ion\Timestamp are really just plain \DateTime objects augmented with Stringable and ION specific formatting.


<?=
new ion\Timestamp(
    
precisionion\Timestamp\Precision::FracTZ,

  
  
// 2022-02-25T16:11:54.118+00:00
  
?>

<?=
new ion\Timestamp(
    
precisionion\Timestamp\Precision::Day

  
  
// 2022-02-25T
  
?>

<?=
new ion\Timestamp(
  
precisionion\Timestamp\Precision::MinTZ,
  
formation\Timestamp\Format::Min,
  
datetime"2020-03-15T12:34",
  
timezone: new DateTimeZone("Europe/Vienna")

  
  
// 2020-03-15T12:34+01:00
  
?>