21 Feb
Posted by: Cal Evans in: JavaScript, PHP, Programming, zend framework
Dear Reader,
Ok, this one is just stupidity on my part but I’m going to post this here so that hopefully others can learn from my mistake.
The problem is simple, JSON encode a PHP object and send it back to the front end. Sounds simple and the last 100 times I wrote this code it was simple. This time, I was too smart for my own good. Here’s the scenario. The object I’m encoding uses PHPs magic functions __get() and __set. __get() and __set() operate on a protected array named (drum roll please) $_data. (Stop me if you’ve heard this one)
class MyClass
{
protected $_data;
public function __construct()
{
$this->_data = array('wifesBirthDay' => '',
'nuclearLaunchCode' => '');
}
public function __get($index)
{
if (isset($this->_data[$index])) {
return $this->_data[$index];
}
return null;
}
public function __set($index, $value)
{
if (isset($this->_data[$index])) {
$this->_data[$index] = $value;
}
}
}
So I instantiate an instance of MyClass and set a few very important properties:
$myObject = new MyClass(); $myObject->wifesBirthday = '5/14'; $myObject->nuclearLaunceCode = 'dontPushThisButton';
Now, var_dump($myObject) returns what you think it would, you can see the protected array and the values.
It was at this point that while I was still able to type coherent code, my brain had checked out for the night. The manual for Zend_Json::encode clearly states:
When encoding PHP objects as JSON, all public properties of that object will be encoded in a JSON object.
Obviously my brain simply chose to ignore this detail.
In my mind, the properties existed…right? Cause I could set them; however, since I’m laying it out here for you, it’s easy to see that since $_data is a protected property, it wasn’t getting passed.
Using FireBug (is there a better FireFox extension? I don’t think so) I could see that my PHP was handing back an empty JSON string to be re-constituted on the client side.
The solution, once I realized what was happening, was quite simple. just create an array of the properties you want to pass back.
$payload = array('wifesBirthday'=>$myObject->wifesBirthDay, 'nuclearLaunchCode'=>$myObject->nuclearLaunchCode);
$output = Zend_Json::encode($payload);
That was my first cut and low and behold it works. However, a better solution came to mind.
class MyClass
{
protected $_data;
public function __get($index)
{
if (isset($this->_data[$index])) {
return $this->_data[$index];
}
return null;
} // public function __get($index)
public function __set($index, $value)
{
if (isset($this->_data[$index])) {
$this->_data[$index] = $value;
return true;
}
return false;
} // public function __set($index, $value)
public function getProperties($skip=array())
{
$returnValue = array();
foreach($this->_data as $key=>$value) {
if (!in_array($key,$skip)) {
$returnValue[$key]=$value;
}
}
return $returnValue;
}
}
There, now I can simply write:
$payload = $myObject->getProperties(); $output = Zend_Json::encode($payload);
If I didn’t want to disseminate the nuclear launch codes (I know I’m gonna start getting some weird searches now) I can write:
$payload = $myObject->getProperties(array('nuclearlaunchCode'));
$output = Zend_Json::encode($payload);
So I hope that by embarrassing myself publicly I can help at least one person. (For the record, it really only took me about 2 minutes to trace down the issue.)
Until next time,
(l)(k)(bunny)
=C=
Tags: FireBug, JSON, PHP, zend framework
9 Responses
fangel
22|Feb|2008 1A theory: Implementing a SPL Iterator for your object will fix the issue.
I don’t know how Zend_Json::encode works, but if it does a foreach($obj ..) then adding a simple SPI Iterator interface to your object will make it work correctly.. If it uses reflections I’m not sure what will happen..
Worth a shot imho..
XOXO
Cal Evans
22|Feb|2008 2Hi fangel!
Thanks for leaving a comment.
IIRC, and I’ll be honest in saying I’ve not looked at the code in a while, Zend_Json uses the naive json_encode method if it’s available. In my case it was. So while my post was specifically about Zend_Json, it applies as well to json_encode().
Also, honestly, I wouldn’t want it to operate any other way. In hindsight, this is the proper way for it to work. I mark properties protected for a reason and it’s real easy to expose them if I need to.
That having been said, yes, if I’m implementing JSON encoding nativly in PHP, it would be possible to do it your way and be able to get a more complete representation of the object.
Thanks again for taking the time to lave a comment.
=C=
PHPDeveloper.org
22|Feb|2008 3Cal Evans’ Blog: I called Zend_Json::encode(), so WTH are all my properties?…
In dealing with a little JSON encoding and objects in a ……
deminy
22|Feb|2008 4Well… Inside the magic function __set() you defined, I think
if (isset($this->_data[$index])) {
should be
if (!isset($this->_data[$index])) {
Cal Evans
22|Feb|2008 5Hi Deminy,
Thanks for posting!
Actually no, it’s that way on purpose but that code was scraped from my real class to build the example. In the real code, I build the array with all the elements blank in the __construct. This way, I can’t accidentally add new properties to the object that would cause me problems later. I’ll correct the example. Thanks for pointing that out.
=C=
fangel
24|Feb|2008 6I did some tests and, if using json_encode, having a Iterator for the object doesn’t help out.
So no, that wouldn’t help on you problem..
I, personally, think I would prefer it to actually work. If you expose a iterator for you object, I would expect any code that needs access to the members of this class to use the iterator. Instead it uses some “magic” code that finds the public members.
Exposing a Iterator, in my mind, means “if you need to loop though the variables in this class - use this Iterator”. But json_encode() doesn’t do this..
-f
Cal Evans
24|Feb|2008 7reHi fangel!
First, I think I may have explained it poorly. Having an iterator does not solve the problem. However, since ALL we are transferring to the front-end is properties, having a method that I can quickly pull out the array of properties and json encode THAT does solve the problem.
Second, json_encode does work the way most of us would expect it to. It iterates through the public properties and encodes them. The mistake I made was to make my array of properties protected…actually, it wasn’t a mistake, it was the right thing to do for the way I was coding.
There is no magic to getProperties() and I would never expect json_encode or Zend_Json::encode() to recognize that method and use it. I was a solution for the problem I had.
Thanks for writing.
=C=
PrettyCoder
25|Feb|2008 8public function getProperties($skip = array())
{
return array_diff_key($this->_data, array_flip($skip));
}
Sorry, couldn’t resist.
Cal Evans
25|Feb|2008 9ZOMG!
Hey, never apologize for doing a better job. I knew array_flip() existed but don’t think I’ve ever used it and I didn’t even know array_diff_key() was there.
Thanks, I’ve updated my code.
=C=
About Me
If you are looking for my contact information, bio, picture, ASL, check out my EPK.
My name is Cal Evans and this is my blog.
Follow me on FriendFeed!
My First Book
Tags
API Apple article Bill Seaver C.C. Chapman Cal Evans CIO Magazine Consulting customer service developers devzone dr. dobbs elizabeth naramore facebook feed flock fun hiring iPod iTunes Kathy Evans linkedin love Marketing nerd herding PHP php abstract phparchitect podcast podcasting poem respect sean coates securephphosting sixty second tech southwest airlines spaz terry chay tivo twitter upgrade valentine wordpress zend zend frameworkMy Content
Categories
Vanity Chart
Get your own chart!
Blogs of friends
Me
Archives
Meta