Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • How to Plan a Website
  • Zend Framework

Posts Tagged ‘JSON’

Zend Framework Piece by Piece

Thursday, September 3rd, 2009

Dear Reader,

Zend Framework is unique in the PHP framework lineup in that it is simple to pull out single pieces and use them independently of the framework itself.

This was the premise of the webcast I presented as part of the summer webcast series, hosted by phparchitect and sponsored by Microsoft.
(more…)

Tags: cache, figlet, JSON, mail, view, zend framework
Posted in zend framework | 10 Comments »

 

I called Zend_Json::encode(), so WTH are all my properties?

Thursday, February 21st, 2008

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
Posted in JavaScript, PHP, Programming, zend framework | 9 Comments »

 
  • Friends of mine

  • My Latest Book


    Avoiding a Goat Rodeo

  • Follow me on twitter!

  • RSS PHP Podcasts

    • Episode 110: Azure hits 1 billion, RubyFlux compiler, Laracasts and more
    • Episode 7: Web Sockets Are Fast
    • Better Documentation for PHP internals – Lately in PHP podcast episode 35
    • Episode 31: Feline Tooth Extraction
    • Episode #2 – Adam Culp
    • Episode 6: PSR-X and the Mexican Standoff
    • Episode 109: Typescript and a bit more…
    • A Better PHP Feature Voting Process – Lately in PHP podcast episode 34
    • Episode 30: It’s Episode 30, you guys
    • PHP Innovation Award Winner of 2012 – Lately in PHP podcast episode 33

  • Me, elsewhere on the Web

    • Best web design company
    • Cal Evans Dot Com
    • Cyrano’s Apprentice
    • Evans Internet Construction Company
    • My Life as a Child
    • PHP Podcasts

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • Book Review
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me, elsewhere on the Web
    • PHP
    • podcasting
    • Programming
    • SQL
    • Technology
    • Web 2.0
    • wordpress
    • WordPress Plugins
    • writing
    • zend framework


Postcards From My Life is proudly powered by WordPress
Entries (RSS) and Comments (RSS).