“Nerfing” a PHP Object

I was trying to think of something with PHP I could blog about that would be short and sweet. Then I thought of something that a good friend of my taught me: nerfing objects.

The Problem

Many times while working with PHP and bigger frameworks, you'll have classes that extend classes that extand classes. We have an ORM system that interfaces our Database with PHP classes. This makes accessing and updating information easy for us. So lets say we have a class called "Post" that stores a blog post. Our ORM class just takes a few lines of code to hook up our "Post" class to the "posts" database tables. We just extend our base DataObject, which contains all sorts of references to other class isntances. Long story short, this PHP code is awesome, but there is one issue.

Lets say I have an AJAX call that gets some post data, so I want to pass my "Post" class through a JSON parser to send back a JSON version of the PHP class. The problem was that the JSON parser was picking up on the extended classes's private members and such, so it was spitting back a whole lot of stuff we didn't want it to that belonged to the ORM classes.

The Solution

So what do we do? We "nerf" the class. Nerfing is a term used by gamers about video games. It means "[to make] a change to a game that reduces the desirability or effectiveness of a particular game element. The term is also used as a verb for the act of making such a change. The term is used as a reference to the NERF brand of toys which are soft and less likely to cause serious injury." (Wikipedia)

So we make a "Nerfed" version of the class, with no functions, or extended protected members, just the data. We do this by using two parts of PHP:

  • stdClass PHP Class
  • PHP's Reflection Class

Just attach this function to your class and you'll be able to "nerf" it.

PHP:
  1. <?php
  2.  
  3. class ExampleClass extends ComplexClass
  4. {
  5.  
  6.   // .. Some PHP Class code ..
  7.   public function GetNerf()
  8.   {
  9.     $nerf = new stdClass();
  10.  
  11.     // Save the values of the fields
  12.     $dbFields = $this->dbFields;
  13.  
  14.     foreach ($dbFields as $n => $v)
  15.     {
  16.       $nerf->$n = $v;
  17.     }
  18.     //var_dump($nerf);
  19.  
  20.     $me_ref = new ReflectionClass(get_class($this));
  21.     $me_properties = $me_ref->getProperties();
  22.     foreach($me_properties as $m)
  23.     {
  24.       if($m->isPublic())
  25.       {
  26.         $name = $m->getName();
  27.         $nerf->$name = $this->$name;
  28.       }
  29.     }
  30.  
  31.     return $nerf;
  32.   }
  33.  
  34. }
  35.  
  36. ?>

Hopefully that makes sense, but the Reflection capabilities of PHP are pretty simple and powerful. Hopefully over the next few weeks I'll be posting some more tricks with PHP Reflection.

Related Posts

  1. jQuery Tip: Better Toggle For many web developers, jQuery is the most awesome JavaScript library out there. For me, it has turned JavaScript from being a nightmare into a power tool. I love JavaScript now, where as before I truely hated it. Takes all the hassel out of most compatibility issues across browsers. I...
  2. Memcached: Simple, Effective, and Powerful Realizing once again I haven't written a blog post for quite some time, I thought I would just write a few smaller posts on things I've learned these last few months. Hopefully I can get back into the habit of blogging regularly again. This last month I finally broke down...
  3. My 2009 Technology Recap Its been two years now that I've been a more or less "serious blogger." I had using the term blogger, since when people hear about blogs, they think of people either detailing their entire lives, or pumping some angel. For me, blogging has just been a way to share information...

Posted in Programming, Web Design. Tagged with , , , , , .

0 Responses

Comments RSS Feed.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.

Powered by WP Hashcash