Get Only Public Class Properties for the Current Class in PHP

PHP provides two built-in functions to retrieve properties of a given class – get_object_vars and get_class_vars. Both these functions behave the same exact way, one taking an object as a variable and the other taking a string class name. The tricky thing about the two functions is that they behave differently depending on the call scope, returning all of the class variables available within the called scope. So if you call either function within the current class you need properties from, all properties are returned – public, protected, and private – because the current scope has access to them all. This makes seemingly simple things like returning all the public properties within the current class a bit of a pain if you want to keep the code inside the class itself.

Continue reading

Why WordPress Should Not Have Won the Open Source CMS Award

Packt Publishing announced the winners for their annual Open Source CMS Award in November, and since then I have been a bit disturbed that the 2009 winner was WordPress. My first reaction was this:

"… So a blogging platform won the content management system award? How sad is that?"

My knee-jerk "how sad is that?" reaction comes not because I don’t think WordPress is worthy, but because of what it implies about the state of other open source CMS projects. The reaction comes from the fact that a blogging platform is kicking your CMS’s ass in its own category.

Continue reading

MySQL Series: Return NULL Values First With Descending Order

Sometimes there are unique situations where you need to order query results by a particular field in descending order, but also need NULL values first. The default (and logical) behavior of MySQL in this case is to return NULL values last, because in descending order they have the lowest value (none). But what if you really need to reverse this and force NULL values to the top of the result set?

Continue reading

CodeWorks 2009 Dallas

CodeWorks 2009 Speaker I was fortunate enough to be selected as the regional speaker for the Dallas CodeWorks 2009 stop by the Dallas PHP User Group through a community voting and selection process. My talk was entitled Object Oriented Apologetics , and was essentially about letting people know what good object-oriented code is, when to use it, how to use it, and more specifically why to use it over traditional procedural PHP code. more

Object Oriented Apologetics

In defense of object-oriented programming – How and why you should use object oriented programming for your next project.This talk is for PHP programmers who are just learning about object oriented code, who cling to old excuses("object oriented code is slower"), or who are otherwise unconvinced of its usefulness. Concrete real-world examples of commonscenarios and challenges that programmers face will be presented, and how taking an object oriented approach is better than a proceduralone in most cases. Copious code examples in both object oriented and procedural approaches will be provided throughout, and thedifferences and benefits of the object oriented approach will be explained.

Download Object Oriented Apologetics on SlideShare

If you were in the talk, please rate it on

The CodeWorks Experience

All in all, the CodeWorks roadshow Dallas stop was much smaller than I expected. There were about 20 people in the talk I gave. I suppose it was both a good a bad thing. On one hand, I had a lot of fun connecting with the other speakers and attendees on a more personal level than I would have had the opportunity to do otherwise. I met a lot of new people in the PHP community that I will probably stay connected with on some level, even if it is just Twitter and IRC. We had a lot of fun the night before the presentation day eating together and hanging out.

On the other hand, I know that as a business, the relatively low attendance levels coupled with the high travel expenses could mean that something like this can’t happen again, which is a shame. This was one of the best efforts I have seen in a while to really lower the price of the conference for attendees by bringing the speakers directly to their cities or at least ones that are close by. Going to ZendCon, for instance, could easily cost up to $2,500 for the whole trip with airfare, hotel stays, and food, if not more.

The bottom line here is that CodeWorks was a good conference that offered a great value for your money. I had a great time meeting and connecting with new people and finally meeting people in real life that I had been communicating with for years. I learned some new things and finally got the kick in the pants I needed to jump into Test Driven Development, thanks to Jason Sweat’s TDD tutorial and hand-holding.

Now I’m just looking forward to php|tek in May 2010.

The One Character Block Comment

When debugging, I often find that I have to comment and un-comment a block of code several times during the process of trying to find out what’s going on. That used to mean typing and deleting comment block characters repetitively, but not anymore. Here’s a simple solution to that problem: Comment or un-comment an entire code block of code by typing or deleting a single character.

I was able to arrive at this solution by combining the one-line comment with the comment block in a way that takes advantage of the rules the different types of comments have to follow.

Continue reading

How Prepared Statements Work, and Why You Should Use Them

Prepared statements differ from normal queries in one major way: Instead of sending one SQL string with the values defined by single quotes in one package, the SQL string and values are sent in two separate calls. Prepared statements themselves are like query blueprints, with placeholders where the values will go. The query values are sent in a separate call with a reference to the prepared statement, and are dropped in place and executed.

This post approaches the problem and presents a solution from a PHP angle, but even if you’re not a PHP developer, you should be able to follow along with some creative substitution.

The Old Way

Prepared statements take longer because they are 2 round-trips to the database for a single query. As soon as you prepare a query, it’s sent to the database with the placeholders you set. So the database engine takes that prepared statement and maps out the query and optimizes it for execution. Then when you call execute() only the values you give are sent to the database, with a reference to that query you just prepared. The database engine drops in the values and runs the query. This is totally immune to SQL injection, because the database engine already knows exactly where the values begin and end (the placeholder marker(s) you set), and therefore never need escaping. The reason SQL injection exists in the first place is because the entire query is interpreted upon execution, values and all. So if anything interferes with the quotes surrounding your values, the engine thinks that value has ended and thus a security hold is introduced. That problem is avoided all together with prepared statements by letting the database engine know ahead of time exactly where to put each value you pass to it later on. There is no need for escaping and there is no need to worry.

Why MVC is so Popular on the Web

The MVC design pattern has been getting a lot of attention in the past few years.  It seems like a new MVC framework pops up every week, and discussions about MVC have become commonplace throughout web programming communities.###MVC is Here to Stay

While MVC is certainly the "new thing" again (funny, because it actually dates back to 1979), it won’t be a passing fad, and it won’t fade quickly – at least not on the web.

Solving the Fundamental Problem of Web Development

Unlike most dektop applications which can be completely coded with one or two different programming languages, websites are a mix of several different programming languages that are constantly changing.  The typical website or web application in composed of no less than 6 different programming languages:

  • Server-side language like PHP/Ruby/Python/.NET, plus:
  • SQL – (Variants: MySQL, MSSQL, PgSQL, SQLite)
  • CSS
  • JavaScript
  • XML (Plus specific formats like RSS and Atom and possibly XSLT)
  • JSON if the web application has an API
  • And who knows what else a few years from now…

As a result, the code naturally has to be separated somehow, and the same content has to be able to be displayed in many different formats (most commonly HTML plus XML and JSON for APIs).

OKC PHP User Group Reboot

The local Oklahoma City PHP User Group is re-starting with the okcCoCo as the new venue. The new meetings will be on the second Tuesday of each month, starting with Tuesday, June 09, 2009 at 6:30pm as the first official meeting. Visit the official OKC PHP User Group website to register for meeting reminders and to connect with other local PHP developers.

I will be presenting my talk on Building a Data Mapper with PHP5 and the Standard PHP Library , followed by a discussion on ORMs and whatever else comes up. The presentation will cover all the thought processes, goals, theories, and actual code that goes into building an ORM (or really any other larger project that requires more advance planning). The project that was the basis of this presentation is phpDataMapper – an open-source PHP5 data mapper ORM layer that I started in the fall of 2008. It now powers the model layer of InvoiceMore, a live web application I launched in March 2009.

This is a presentation I have given before at Tulsa TechFest.

Watch the video of this presentation online

jQuery UI Datepicker with AJAX and LiveQuery

I’ve been a little aggravated lately trying to get jQuery UI Datepicker to work correctly on dynamically added fields for creating additional line items to invoices for InvoiceMore. It works great for fields already displayed on the page, but it tends to have major issues with dynamically added fields through AJAX or AHAH. Of course it won’t work out of the box with elements added dynamically to the DOM, so we can use jQuery’s $.live() event (new in 1.3 – you previously had to use liveQuery) to make it work. The Datepicker works by binding to the focus() event by default, but as of jQuery 1.3.2, the ‘focus’ event cannot be monitored by the ‘live’ event function. So we’re stuck with a little work around:

You would think just a simple "$(this).datepicker()" call wrapped inside the live() event would work, but it doesn’t. Turns out that in order to get it working consistently, you have to add the ‘showOn: focus’ config option as well as manually focusing on the element with the focus() event. Charming.

InvoiceMore Launch and Differentiation


InvoiceMore, the startup I have been working on in my spare time for over 7 months, has finally launched. This post actually comes a bit late to the party, because I actually launched InvoiceMore at OpenBeta on March 12, 2009 and blogged about it on the Actridge blog that day. I haven’t even had time to thinkabout sitting down to write this post on my personal blog about the launch until now. That’s a testament to how crazy busy my life has been since I decided to pour all my spare time into starting a business. So what is InvoiceMore, and how is it different?

moreInvoiceMore is an online billing and invoicing application aimed at freelancers and small businesses. It basically provides a super-simple web interface for creating and sending invoices to clients and recording payments for them. You can email and generate PDF invoices, print and snail mail them, and just keep track of your clients and their payments in a really easy and intuitive way. It was created based on my experience from a different billing application I created to fill my own client billing needs for freelance and contract work.


A lot of people ask me why I made InvoiceMore, and how it will be any different from what’s already out there on the market. If you’ve ever used an online billing application, or currently are using one, InvoiceMore works much the same way, with one major exception: Recurring billing. All of the online web-based billing applications I have come across so far do recurring billing the same way: a “recurring invoice template” that has a recurring interval set on it, like “1 month” or “2 weeks”. The problem is, if you have a client with multiple recurring services at different intervals, you have to setup multiple recurring invoice templates, and your client ends up getting more than one invoice per month at least a few months of the year.

Clients don’t ask for recurring Invoices. They ask for recurring products and services. An invoice is the natural end result of the products and services they buy. Competing billing applications make you create and setup what should be the end result: the Invoice. So to solve this problem, I built recurring billing in InvoiceMore in what I believe is a much more natural way: the products and services themselves. So with InvoiceMore, you associate products and services with clients and pick a recurring interval for that association. Then every billing cycle, invoices are automatically generated for that client from the recurring products and services that are due sometime within that billing period. You end up with a single invoice with everything due on it instead of multiple “recurring invoice templates” that are generated and sent independently.

So if you’re interested in learning more, you can try InvoiceMore out for free, or just read the information on the website . Let me know what you think in the comments here, or on the official UserVoice page for feedback and ideas.