PHP code example of academe / serializeparser

1. Go to this page and download the library: Download academe/serializeparser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

academe / serializeparser example snippets


// Create a complex array/object/string/number/boolean to serialize.

$obj = new \Academe\SerializeParser\StringReader('xyz');
$obj->foo = true;

$data = [
    'a' => 1,
    [
        'foo' => 'bar',
        $obj,
        'b' => false,
    ],
];

$serialized = serialize($data);

// Take a look a how PHP has serialised it.

echo $serialized;
// a:3:{s:1:"a";i:1;i:0;a:3:{s:3:"foo";s:3:"bar";i:0;O:36:"Academe\SerializeParser\StringReader":4:{s:6:"*pos";i:0;s:6:"*max";i:2;s:9:"*string";s:3:"xyz";s:3:"foo";b:1;}i:1;O:7:"myClass":2:{s:12:"*protected";s:4:"prot";s:16:"myClassprivate";s:4:"priv";}}s:1:"b";b:0;}

// Somewhat beautified:

/*
a:3:{
    s:1:"a";i:1;
    i:0;a:3:{
        s:3:"foo";s:3:"bar";
        i:0;O:36:"Academe\SerializeParser\StringReader":4:{
            s:6:"*pos";i:0;
            s:6:"*max";i:2;
            s:9:"*string";s:3:"xyz";
            s:3:"foo";b:1;
        }
        i:1;O:7:"myClass":2:{
            s:12:"*protected";s:4:"prot";
            s:16:"myClassprivate";s:4:"priv";
        }
    }
    s:1:"b";b:0;
}
*/

// Now parse it to look at what it inside, without instantiating the
// original objects in it.

$parser = new \Academe\SerializeParser\Parser;
$parsed = $parser->parse($serialized);

var_dump($parsed);

/*
array(3) {
  ["a"]=>
  int(1)
  [0]=>
  array(3) {
    ["foo"]=>
    string(3) "bar"
    [0]=>
    object(stdClass)#6 (5) {
      ["__class_name"]=>
      string(36) "Academe\SerializeParser\StringReader"
      ["pos"]=>
      int(0)
      ["max"]=>
      int(2)
      ["string"]=>
      string(3) "xyz"
      ["foo"]=>
      bool(true)
    }
    [1]=>
    object(stdClass)#7 (3) {
      ["__class_name"]=>
      string(7) "myClass"
      ["protected"]=>
      string(4) "prot"
      ["private"]=>
      string(4) "priv"
    }
  }
  ["b"]=>
  bool(false)
}
*/