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
class ExampleClass extends ComplexClass
{
// .. Some PHP Class code ..
public function GetNerf()
{
$nerf = new stdClass();
// Save the values of the fields
$dbFields = $this->dbFields;
foreach ($dbFields as $n => $v)
{
$nerf->$n = $v;
}
//var_dump($nerf);
$me_ref = new ReflectionClass(get_class($this));
$me_properties = $me_ref->getProperties();
foreach($me_properties as $m)
{
if($m->isPublic())
{
$name = $m->getName();
$nerf->$name = $this->$name;
}
}
return $nerf;
}
}
?>
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
- PHP Singletons, Sub-Classing, and HAS-A Relationships I’ve been very busy these last fews weeks and have neglected making any posts. While there are a variety of subjects I’d love to post about, they’ll most likely have to wait until the next year. However, I thought I might be able to throw up a quick example of...
- Aptana Studio – PHP IDE Alternative to PDT, Zend Studio The other day I ran into another option as an IDE for PHP. If you’ve been following my blog, I’ve been a big user of Eclipse PDT (http://www.eclipse.org/pdt/) and Zend Studio (http://www.zend.com/en/products/studio/). There is a third one to add to the list, Aptana Studio. It is built on-top of the...
- List of 50 PHP Tools Found a great article listing popular tools that every PHP developer should be away of. Here is an excerpt: PHP is one of the most widely used open-source server-side scripting languages that exist today. With over 20 million indexed domains using PHP, including major websites like Facebook, Digg and WordPress,...
- Characteristics of Good PHP Code At the time I am sitting in the Las Vegas airport waiting for a very delayed layover flight to San Diego. Wonderful weather causing all sorts of headaches. So I’ve decided to make the best of my time by writing the introductory article to a series of PHP articles. I’ve...
- PHP Design – Biggest Database Oversights Over the last three years I’ve had the opportunity to work on several PHP projects, some of them having grown rapidly and required to scale quickly. Three in particular have been a fantastic learning experience for me. Now I don’t consider myself a total expert, but I thought I would...