Friggin objects nested in PHP arrays

Monday, August 18, 2008, 10:45 PM
Source Code by John

Anyone who visits my blog much knows I'm not the world's biggest fan of using frameworks or even someone else's code. Yeah, I've tinkered with WordPress a bit, that's mostly to drag out what really makes it effective from an SEO standpoint.

I am currently working on a project that makes use of the US Postal Service's Web API Tools. The US Postal Service requires that you jerk around with their shockingly limited testbed server before going whole hog onto their production server. In the interest of not pulling my hair out with experimenting on this very limited testbed server, I opted to Google some of the API code.

Advertisements


But, the code itself dumps objects nested inside arrays that don't have very useful keys. OK, find some more code, right? Wrong. A lot of the API-ready code doesn't seem to work. Hahaha. So, a crappy solution is better than no solution, right?

Proceeding with the crappy solution, I had to dig the objects out the array. This means going through the array recursively -- something PHP does piss poorly at best -- and identifying the objects and then converting them to more usable variables.

One function helps tremendously in this task: get_object_vars.

Get_object_vars is a PHP function that loads your objects into an array. It's not always the most useful functions, since obviously well coded objects should actually be simple and easy to use. A good object should work like $whatever->subwhatever and away you go, right? But, not all objects are good objects. Especially when they are nested in arrays that have little or no identifying keys and the keys change dramatically based on what they're loading.

That's where something like get_object_vars comes in. You've dumped your whole array and you run into a roadblock. It's an object. Instead of desperately trying to make the object work, which can be hard to do if it isn't your code and the code is poorly documented, you can just dump the object to an array and the go through it recursively.

Is that good form for coding? Nope. It's terrible form.

But, one thing you find out about coding for a living, especially in a meat market like web design, is that whatever gets the job done is right. Especially if you can ensure it doesn't have any security flaws.

I've seen lots of well-formed object-oriented code that just doesn't work well. Hell, look at Digg's recent problem with unscrubbed inputs!

I think this is one of the reasons I hate the religion of so-called good coding. Well-formed code can still be garbage. And poorly-formed code can still run through hackers like a tank. Now, that's a little more of a rant than the topic deserves, so I'll leave that at that.

Whatever the case and whatever your stance, get_object_vars is a handy tool.


Mail article to a friend

© 2008 Pro Content and Design. All rights reserved.


Tools

Check Google PageRank


Recent articles

  1. 301 redirects, and the importance of keeping SERP mojo
  2. Clients chasing white whales
  3. Hitting your website with a pipe
  4. Search engine results land on some odd spots
  5. Friggin objects nested in PHP arrays
  6. PageRank from a single link over many high PR sites
  7. PlayStation 3 browser pops up in logs
  8. eBay will be the first big, successful Web 1.0 company to die
  9. It's awesome when your joke takes off
  10. Domain name generator, plus WHOIS and PageRank features

Welcome!

Wonder where to start with your web design business?

This blog follows along with my efforts to build and grow a website design business, Pro Content and Design.

The goal of this blog is to fill in blanks that may be empty as you get your business rolling.

This blog, particularly the source code section, is not intended for beginners. If you are not comfortable with databases, Ajax, DOM objects and other advanced methods, I strongly suggest you go take a look over at W3 Schools before even reading -- let alone tinkering with -- any of the code here.

I hope this blog has some value to web designers as they attempt to get their businesses going.

Good luck, and happy reading.

Thank you,
John Crawford
Pro Content and Design

Books


I highly recommend Art of the Start if you have no idea where to start with marketing.

Links

Coding
W3 Schools
IBM's Mastering Ajax Series

Graphic Design
Worth 1000
Stock.XCHNG
Urban Fonts

Website Software
Apache Web Server
SquirrelMail
PHP/Zend

Website Design Issues
Non-Standard Character Guide
Google Trends
Search Engine Optimization Analyzer

Business
Guy Kawasaki's Blog
Seth Godin's Blog
Freakonomics

Computers
NewEgg

My Main Website
Pro Content and Design

Websites I have built
PunxsyPage: local free classifieds website

Farm N Land: low-cost real estate listing website

InvestYoung: semi-defunct finance blog

Groundhog Festival: for the local summer festival

Weather Discovery Center

My Webapps
TV Stations Transmitter Database

Google PageRank Checker

Website where I did the code, database and admin
Tour de Toona: annual bicycle race in Altoona, PA