note 96248 modified in function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Wed, 17 Feb 2010 14:51:12 +0000 |
| Subject: | note 96248 modified in function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166064@lists.php.net to get a copy of this message | ||
If I want to encode object whith all it's private and protected properties, then I implements
that methods in my object:
<?php
public function encodeJSON()
{
foreach ($this as $key => $value)
{
$json->$key = $value;
}
return json_encode($json);
}
public function decodeJSON($json_str)
{
$json = json_decode($json_str, 1);
foreach ($json as $key => $value)
{
$this->$key = $value;
}
}
?>
Or you may extend your class from base class, wich is implements that methods.
Found that much more simple than regular expressions with PHP serialized objects...
--was--
If I want to encode object whith all it's private and protected properties, then I implements
that methods in my object:
public function encodeJSON()
{
foreach ($this as $key => $value)
{
$json->$key = $value;
}
return json_encode($json);
}
public function decodeJSON($json_str)
{
$json = json_decode($json_str, 1);
foreach ($json as $key => $value)
{
$this->$key = $value;
}
}
Or you may extend your class from base class, wich is implements that methods.
Found that much more simple than regular expressions with PHP serialized objects...
http://php.net/manual/en/function.json-encode.php