PHP code example of porkchopsandwiches / preserialiser

1. Go to this page and download the library: Download porkchopsandwiches/preserialiser 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/ */

    

porkchopsandwiches / preserialiser example snippets


use PorkChopSandwiches\Preserialiser\Preserialiser;

$p = new Preserialiser();
$p -> preserialise(1); # => 1
$p -> preserialise("string"); # => "string"
$p -> preserialise(array(1, true, "three")); # => array(1, true, "three")

$obj = new stdClass;
$obj -> prop = "value";
$p -> preserialise($obj); # => array("prop" => "value")

class ExampleA {
	private $a = "foo";
	public $b = "bar";
}
$p -> preserialise(new ExampleA()); # => array("a" => "bar")


use PorkChopSandwiches\Preserialiser\Preserialiser;
use PorkChopSandwiches\Preserialiser\Preserialisable;

class ExampleB implements Preserialisable {
	private $a = "foo";
	private $b = "bar";
	
	public function preserialise (array $args = array()) {
		$data = array(
			"a" => $this -> a
		);
			
		if (array_key_exists("

use PorkChopSandwiches\Preserialiser\Preserialiser;
use PorkChopSandwiches\Preserialiser\Preserialisable;

class ExampleParent implements Preserialisable {
	private $children = array();
	
	public function addChild(ExampleChild $child) {
		$child -> setParent($this);
		$this -> children[] = $child;
	}
	
	public function preserialise (array $args = array()) {
		$data = array(
			"type" => "parent"
		);
		
		if (array_key_exists(""] = $this -> parent;
		}
		
		return $data;
	}
}

$p = new Preserialiser();
$parent = new ExampleParent();
$child = new ExampleChild();
$parent -> addChild($child);

$p -> preserialise($parent);
# => array("type" => "parent")
$p -> preserialise($parent, array("