About the Resource Model

It is November 2015, as I write this article, PHP is finalizing the release of PHP 7.0. This release promises features that will greatly improve server performance.

Unfortunately, for me, the release will break a great deal of my existing PHP code. Specifically, the release will remove the mysql_query() which is used for database connections.

PHP deprecated the mysql_ family of functions and and replaced them with a much better program called PHP Data Objects (PDO) way back in 2004 with the release of PHP 5.

I prefer accessing data through an object; So I was really excited about th is change and threw hundreds of hours into developing various object models built around the PDO object and failed to find or create a model for using PDO that I felt was elegant. In ten years I have found or developed a framework around PDO that I feel comfortable putting on my server much less selling to a client.

The problem I face has to do with the scope of variables in PHP. In PHP, functions are global while objects are local to the scope in which they were created. The flow of a PHP 3 program worked as follows:

Near the beginning of the program one would connect to the database with mysql_connect(), then use mysql_query() whenever the program needed data from the server.

Flow for the PDO object is awkward because objects are local to the scope in whichy the were created. Creating an instance of the object at the beginning of the code is problematic because one has to add convoluted code to access the object throughout the program.

The release of PHP 7.0 is traumatic for me because the removal of the mysql_ drives the point that, in 10 years, I've yet to come across an implementation of the PDO that I think is elegant, and I have looked at a lot of code.

For the last ten years, I've labored under the assumption that, somehow, I just don't get it. My inability to find an implementation of the PDO object that didn't turn my stomache indicated that there was a problem with me.

Earlier this year, I was wading through the garbage posted on stackoverflow.com. A user asked what is the best way to pass a large array of data into a SQL table using PDO. The question included a snippet of code.

The responses failed to address the question, but launched into accusations that the person posting was using "anti-patterns" in his code.

I was extremely upset at the thread. I often run into situations where I have large blocks of data I want to place into a database and I want to know what programmers consider to be the best way to process large arrays of data through PDO.

Since this incidence, I discovered numerous threads of StackOverflow and other forums where discussions where discussions where interrupted and even stopped by the word "anti-pattern."

People on programming forums appear to be scared of the term "anti-pattern."

Political history is rife with situations where people are conditioned to fear certain words. The fear of a word creates a climate in which the ruling elite is able to control the people through fear.

Back to the motivation for this work.

The Resource Model to Web Design holds that certain parts of a program, such as database connections, should be seen as global resources available throughout a program.

The model that I advocate is considered, in certain circles, to be a [gasp] "anti-pattern."

In this work, I want to go a step beyond simply presenting a discussion about different approaches to web design. I want to directly confront those who seek to put down other programmers with buzzwords "patterns" and "anti-patterns."