PHP Classes

File: examples/pair.php

Recommend this page to a friend!
  Classes of zinsou A.A.E.Moïse   PHP Dictionary to Array class   examples/pair.php   Download  
File: examples/pair.php
Role: Example script
Content type: text/plain
Description: example script
Class: PHP Dictionary to Array class
Manipulate value collections as arrays or objects
Author: By
Last change:
Date: 5 years ago
Size: 2,791 bytes
 

Contents

Class file image Download
This is a naive but functional Pair data structure implemented with only xDict:<br>
<?php
highlight_string
('
<?php
require_once(\'./../xdict.class.php\');
class Pair{
    private $xdict=null;
    public function __construct($key,$value){
    $this->xdict=xdict(0,\'\',false,false,2);
    $this->xdict[\'key\']=$key;
    $this->xdict[\'value\']=$value;
    }

    public function copy(){
        return clone($this);
    }
    public function isEmpty (){
        return $this->xdict->isEmpty();
    }

    public function jsonSerialize() {
        $anonymous=function(&$v){
            if(is_object($v)) $v=serialize($v);
            if(is_resource($v)) $v=get_resource_type($v).\'_#\'.@intval($v);
        };
        $x=$this->xdict->container;
        array_walk_recursive($x,$anonymous);
        return $x;
    }

    public function toArray(){
        return $this->xdict->toArray();
    }
   
    public function __debugInfo(){
        return $this->xdict->container;
    }

    public function __get($property){
        if($this->xdict->offsetExists($property)) return $this->xdict->offsetGet($property);
    }

    public function __set($property,$value){
        if($property===\'key\'){
            $this->xdict->offsetSet($property,$value);
        }elseif($property===\'value\'){
            $this->xdict->offsetSet($property,$value);
        }else{
            return;
        }
    }

    public function __clone() {
        $this->xdict = clone ($this->xdict);
    }

}


echo \'<pre>\';
$a = new Pair("a", 1);
$b = $a->copy();

$b->key = "u";

var_dump($a);
var_dump($b);
?>'
);


require_once(
'./../xdict.class.php');
class
Pair{
    private
$xdict=null;
    public function
__construct($key,$value){
   
$this->xdict=xdict(0,'',false,false,2);
   
$this->xdict['key']=$key;
   
$this->xdict['value']=$value;
    }

    public function
copy(){
        return clone(
$this);
    }
    public function
isEmpty (){
        return
$this->xdict->isEmpty();
    }

    public function
jsonSerialize() {
       
$anonymous=function(&$v){
            if(
is_object($v)) $v=serialize($v);
            if(
is_resource($v)) $v=get_resource_type($v).'_#'.@intval($v);
        };
       
$x=$this->xdict->container;
       
array_walk_recursive($x,$anonymous);
        return
$x;
    }

    public function
toArray(){
        return
$this->xdict->toArray();
    }
   
    public function
__debugInfo(){
        return
$this->xdict->container;
    }

    public function
__get($property){
        if(
$this->xdict->offsetExists($property)) return $this->xdict->offsetGet($property);
    }

    public function
__set($property,$value){
        if(
$property==='key'){
           
$this->xdict->offsetSet($property,$value);
        }elseif(
$property==='value'){
           
$this->xdict->offsetSet($property,$value);
        }else{
            return;
        }
    }

    public function
__clone() {
       
$this->xdict = clone ($this->xdict);
    }

}


echo
'<pre>';
$a = new Pair("a", 1);
$b = $a->copy();

$b->key = "u";

var_dump($a);
var_dump($b);