Skip to content

Instantly share code, notes, and snippets.

@mpcm

mpcm/error.php Secret

Created March 24, 2016 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpcm/2ce3687b121fdd13e2a8 to your computer and use it in GitHub Desktop.
Save mpcm/2ce3687b121fdd13e2a8 to your computer and use it in GitHub Desktop.
old jsonobject from code.google.com archive
<?php
require('jsonobject.class.php');
echo '<pre>';
$json_in ='{"":"Blank","A":"a","B":"b","C":"c"}';
echo "json string: $json_in\n";
echo "\n\n";
echo "json_decode(*): \n";
print_r(json_decode($json_in));
echo "\n\n";
echo "json_decode(*,true): \n";
print_r(json_decode($json_in, true));
echo "\n\n";
echo "ArrayObject:\n";
print_r(new ArrayObject(json_decode($json_in, true)));
echo "\nNotice: /home/~/public_html/jsonobject/branches/error.php line 19 - Illegal member variable name
\n";
echo "jsonObject: \n";
print_r(new jsonObject(json_decode($json_in, true)));
echo "\n\n";
echo '</pre>';
<?php
require_once('jsonobject.class.php');
$json_in ='{"":"Blank","A":"a","B":"b","C":"c"}';
echo "json string: ". $json_in . "<BR/>";
$a = json_decode($json_in);
$b = json_decode($json_in, true);
echo 'print_r() output<HR/>An array with an empty key:<pre>' . print_r($b, true) . "</pre><BR/><BR/>\n\n";
echo 'Has the correct keys returned from array_keys:' . print_r(array_keys($b), true);
$b_as_object = (object) $b;
echo echo_output($b_as_object, "<HR/>Standard Object Casting:<BR/>\n");
echo '<BR/>Has the correct keys returned: ' . print_r($b_as_object, true);
echo '<BR/>But throws: Notice: /home/mpcm/public_html/jsonobject/branches/example.php line 24 - Illegal member variable name';
$b_as_jsonobject = new jsonObject($b);
echo echo_output($b_as_jsonobject, "<BR/><BR/><BR/>\n\n<HR/>jsonObject Instance:<BR/>\n");
echo '<BR/>Has the correct keys returned:<BR/>' . print_r($b_as_jsonobject, true);
function echo_output($c, $s = ''){
foreach($c as $k=>$v){
$s .= "[$k] =>". $c->{$k} ."<BR/>\n";
}
return $s;
}
<?php
class jsonObject implements Iterator{
private $data = array();
public function __construct($array){
$this->data = $this->recursive_Replace($array);
}
public function json_encode()
{
return json_encode($this->recursive_Restore($this->data));
}
private function __set($key, $value){
$this->data[$key] = is_array($value) ? $this->recursive_Replace($value) : $value;
}
private function __get($key){
if(isset($this->data[$key])) return $this->data[$key];
throw new Exception('Missing Key:'.$key);
}
private function recursive_Replace($o){
foreach($o as $k => $v){
if(is_array($o[$k])) $o[$k] = new jsonObject($v);
}
return $o;
}
private function recursive_Restore($o){
foreach($o as $k => $v){
if(is_object($v)) $o[$k] = $this->recursive_Restore($v->data);
}
return $o;
}
public function rewind() {
reset($this->data);
}
public function current() {
return current($this->data);
}
public function key() {
return key($this->data);
}
public function next() {
return next($this->data);
}
public function valid() {
return $this->current() !== false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment