PHP: Environments, Libraries, and Applications – Oh My!

May 24th, 2009 by Ralph Schindler

Over the past 10 years or so, I’ve worked with many different code bases and libraries. Originally, the “libraries” were my own because in my earlier programming days, I had a bad case of “NCH” syndrome. That’s “Not Coded Here” syndrome for the uninitiated. As time had gone on, there were some solutions that I needed for a simple project and did not have the time nor the patience to develop a custom library for. That’s when I started relying on others experience and code to get me through projects.

The first “library” I remember using was px.sklar.com by David Sklar. There were some great components in there that were worth integrating into projects, but I hesitate to call it a true library though since its both a repository of both reusable components as well as complete solutions/applications. Moving on into the 21st century, a more “official” PHP library was being born; the PEAR project. The first component I really started depending on for many projects was the Spreadsheet_Excel_Writer. PEAR is not without issues of its own, but thats a topic for a separate article.

A Little History

My earliest PHP applications where fairly simple. A PHP page that would interact with a database, and render some html. Looking back at them, they all look like oodles of hacks and spaghetti code. Of course this was 1999ish, so it was OK because after all, it got the job done. As projects grew larger, so did a desire for better organization. This new wave of applications I was writing at the time was the first divergence from Model 1 applications, and came with the introduction of the second library I started using.

Smarty (which used to be part of the PHP Project), was a library I came to depend on in every project. The single greatest aspect of Smarty from a code organization standpoint was that it separated scripts into “business logic” scripts and “presentation logic” scripts. If an application was a soup of code, Smarty was the tool which divided out the presentation specific code, or what we’d call the ‘view’ in the MVC paradigm, from the business specific code, or what we’d call the controller and model in the MVC paradigm. This was the first step many took towards what is known in the JSP world as Model 2 programming.

So why this history wrapped in with a little personal experience? Well, I’d say the path I have followed is pretty typical of programmers that use scripting languages to build applications, specifically web-applications. That said, as the technologies we’ve used evolved and grown.. we tend to move towards solutions that offer a sense of best practices, better code organization, and most importantly- reduce the time to market.

What does that have to do with you? Well, I’ve seen my share of PHP centric projects come and go. In addition to those projects, I’ve kept a watchful eye on projects in other communities such as the Ruby, Perl, Java and .NET communities. From them, we’ve borrowed concepts, ideas and tools to create better solutions for the PHP community. With that, I’ll continue on with explaining several of the most common facets of any PHP project. If this seems basic at first, its actually laying the groundwork for a few more in-depth articles down the line.

What is an Environment?

In PHP, the environment is the set of resources, capabilities and settings for immediate use within the lifespan of any one php process. I know thats a very general statement, but lets explore that a bit. On most systems, you’ll find a php.ini file. This ini file generally sets values for the php process to initialize with when it starts up. Some of these can be modified by the SAPI (command line layer, apache layer, etc), while other can be modified during runtime via set_ini, and others cannot be modified at all.

Each time a script is executed, it first inherits these php.ini values. This means, by default, if none have changed, a script is subject to the rules defined by the php.ini on the system. If these values (php.ini system values) are out of your control, this means that the script running has an ambiguous initial environment. This environment might have been defined by the system administrator or by the packager of the php distribution you are using.

If you are subject to an ambiguous environment setup, there are greater the chances your application will fail upon setup or during execution. At least one of these situations has come to plague a PHP developer at one time or another:

  • display_errors might be off, causing a WTF moment when an error arises.
  • error_reporting level is set to E_STRICT and the script was not written with respect to the error_reporting including this mode, thus creating 100′s of notices.
  • open_basedir was set and your script doesn’t have access to some resources it expects to have access to.

Those are just 3 of the more popular examples stemming from 3 different keys that can be set within a php.ini. To put it in a bigger perspective: there are 100s of these values. The point that needs to be most impressed is that for any given php script or php application, it should either check the environment at script startup, or in the least provide all of the environment prerequisites and assumptions the script or application makes. The ideal solution is to supply a script that will check the environment and report at installation time if the ini values are correct.

One of the more interesting environment variables in PHP, much like other languages and systems, is the common path. In PHP, the common path is called the include_path. The include_path just might be the most important php.ini based value to any script or project. During a PHP scripts runtime, the loading of files and components are generally checked against the paths defined within the include_path. This means that any scripts or classes (effectively any PHP code) can be located and loaded with a relative path, a path that is relative to any of the paths defined in the include_path.

The include_path is a pretty powerful thing. It makes it easier to bundle components and packages into “libraries”, and use them within projects. This helps facility DRY principals by encouraging good code reuse and solid library design. On the other hand, if you don’t properly manage your libraries that are on your include_path, this could pose some pretty significant problems down the line. More on that later though.

The general rule of thumb is this: take control of the php process’s environment as much as possible to ensure consistent behavior.

What is a Library?

Its seems like library is a fairly generic term, but I want to add some specific meaning to it at least in terms of PHP. A general definition of a library would effectively be a “collection of reusable code”; and that statement is true for all intents and purposes. For the purposes of this article, I’d like to take that a little further.

A library is a collection of components. While a library solves a less specific general problem, components solve a more specific general problem. Get it yet?

For demonstration purposes, I’ll use the Zend Framework.. since I’m a little biased towards that one. The Zend Framework has a couple of libraries, the main one called the Standard Library. The ZF Standard Library solves a pretty general problem: “The PHP Application problem”. As you can see, thats a fairly general (relatively speaking) problem it attempts to solve. This library is made up of several components that solve specific problems within the “PHP Application problem.” For example, Zend_View and Zend_Controller solve the “web application structure” problem. Zend_Form solves the “web forms” problem. So on and so forth. These are problems that can be solved with tried, tested, and true solutions. These solutions can generally be considered “best practices“. They are solved so that you can get onto solving the even more specific problems… those inside the “application”.

Its worth noting that the definition of a library is also relative to the audience its targeted at. In our above example, the Zend Framework’s intended audience is all PHP developers. Your company, on the other hand, has a smaller target audience: its internal developers. Since that audience is a smaller and more concise group, their needs are more specific than those of the global developer community. That means that a company’s “library” might solve “more specific general problems” on a company wide scale. For example, a company might have 10 applications that use a single-sign-on system. Since those 10 applications within that company have the less specific problem of user sign on, that solution would be best fitted inside the company’s “library”.

In general, libraries solve problems that are generic enough for the entire intended audience, and each problem solved into a component of the “library”. Everything else goes into your “application”.

What is an Application?

As hinted above in the section on libraries, an application too is defined by the problem it attempts to solve. An application is a collection of business specific code which solves a very specific business problem. Again, this sounds generic, but it can be further defined and explained.

A business problem is the most specific problem that can be solved with code; this is the application. It will be the sum of all target environments, target audiences, and target tasks that should be solved. These business problems have a very narrow focus. While applications can be further defined into specific areas of code, the whole of the application’s object is to solve the business problem.

Depending on how complicated the business problem is that is target of the application to solve; an application might be modular. If an application is modular, that implies that the application’s problem area can be divided into even more specific areas of code with specific responsibilities. Lets take a community website for example. The site might include forums, user management, mail, calendaring and news. Each of these respective areas of the site could be considered modules of the main application or website. While this is a generic example, it does demonstrated a logical division of responsibility which is ultimately the point of introducing modules into an application. Each project and business should evaluate their application and decide upfront how granular the application’s problem is, and how best to further divide it. Doing this up front will alleviate many issues that could arise later as the code base starts to grow.

Beyond the modularity of an application, a further, more logical division and organization of code is generally applied. While there are several paradigms of application organization, we’ll focus on the MVC architecture (if you are not familiar with the MVC architecture it might be best to read the wikipedia article first before moving forward). Both an applications module and a non-modular application can be organized into Models, Views, and Controllers.. the main constituents of the MVC paradigm. Without getting to involved into what MVC is, one should know that:

  • The model represents the code base for solving the business problem at hand in a UI and environment agnostic way.
  • The controller represents the code base responsible for bridging a user’s interaction with the UI to the business model, and setting up new UI.
  • The view represents the code base responsible for creating the environment specific UI.

The above grouping of purposes is what is called as a separation of concerns.

Recap

Here is a recap of the terms defined within this article:

  • An Environment is the sum of all resources, capabilities and settings that exist in a PHP process. This generally includes what extensions and ini settings are preset for the PHP process.
  • A Library is collection of code that solves a less specific problem which is further defined by the libraries target audience and problem area.
  • A Component is a collection of code that solves a more specific problem within a library.
  • An Application is collection of code that solves a specific business problem. Ideally, applications consume libraries and components to facilitate quicker and more standardized development.
  • A Module is a collection of code that solves a more specific atomic problem of the larger business problem. The sum of all modules within an application attempt the solve the larger business problem.
  • MVC is a way to group code within both a module and application into a code base that facilitate a better separation of concerns.

PHPAustin Meetup Slides – Software Engineering In PHP

May 15th, 2009 by Ralph Schindler

On Tuesday, Josh Butts and I gave a presentation at the monthly Austin PHP Meetup titled “Software Engineering In PHP”.  Around 30 people were present and judging by the number of questions that were raised on each slide, the interest in the subject matter was fairly high.  In the end, it took around 2:15 to get through the 35 or so slides.

Read the rest of this entry »

The Semi-Official Zend Framework Pear Channel

January 7th, 2009 by Ralph Schindler

Pear Channel?

For the past few months, the ZF team has been playing with the idea of releasing ZF from a PEAR channel. Over the past 2 years, we have seen a few channels distributing ZF that have pop up here and there.. so that lead us to believe there is an itch that needs scratching.

The compelling reason against a PEAR channel is that, with ZF, there is nothing to “install”. Just pop ZF in your include_path and off you go. You could obtain ZF from SVN via export, checkout or externals tag.. or you could download from the website. A PEAR channel (until recently), didn’t make enough sense because copying files from one location to another was all it would be doing.

ZF Grows beyond Component Library

That is … until ZF 1.8 (coming soon to developers near you). With 1.8, Zend_Tool will be going into production. I’ve chatted (#zftalk.dev@freenode) about it, I’ve spoke about it (#zendcon08), and I’ve tweeted about it in recent months. But for those that don’t know, I can sum Zend_Tool up in 3 major aspects of functionality:

  • Zend_Tool_Framework is a dispatch system. While Zend_Controller has the Front Controller and web model hammered down pretty good, Zend_Tool_Framework is an introspective dispatch system for exposing its capabilities via command line (cli), XML-RPC, SOAP, or any other [insert your remoting platform of choice here].
  • Zend_Tool_Project is a profile driven system for managing project related resources and their relationships to one another, the ability to create them, remove them and alter them within the lifecycle of a projects development.
  • Zend_Tool_CodeGenerator is an abstracted system for generating code, including but not limited to PHP. Plans are in the works for generating Apache configuration files, ini and xml configuration files… all wrapped up in an API that is natural and similar to the API’s you’ve already become accustom to inside ZF.

So, that said.. What does this have to do with the PEAR channel? ZF is moving from a library of “runtime components” into more of a holistic framework with capabilities of code-generation, scaffolding, and project management, which complicates the process of installation. PEAR installer is really good at installing code into an already running PHP stack, be it site wide or local. So, by delivering ZF through the PEAR channel, the complexity of installation is shifted off of the consumers and onto the delivery channel.

So what does “installing” mean? It means some elements of the package need to go into some pretty specific areas on your system for them to work correctly. For ZF, it means you will need to put zf.sh or zf.bat in your executable path, zf.php in the php_bin directory, and put the Zend Framework inside your include_path. If you’ve used tools like PHPUnit, PHPDoc, or some other framework, this type of “installation” should make sense to you. If not, go poke around you system after installation to better understand.

Details

So, onto the technical details. If you want to see what it can do, first discover and install:

(discover the zf channel)
/my/path# pear channel-discover pear.zfcampus.org

(install zf-devel)
/my/path# pear install zfcampus/zf-devel

(or for something stable)
/my/path# pear install zfcampus/zf

More information will be posted on http://pear.zfcampus.org as it becomes available (this includes other packages in the channel, and other releases like beta and alpha).

To see Zend_Tool in action:

/my/path# mkdir tmp; cd tmp;
/my/path/tmp# zf create project

Now, go explore the project that was created. In addition to that, you can also run “zf show profile” and it will generate a tree of your project. There will be more updates, and more providers available in the coming weeks to show off what we’ve been developing for Zend_Tool. Also keep Zend_Application in mind because as it formalizes, it will be the target of what we will be generating from Zend_Tool and the zf command line interface.

Details, Details, DETAILS!

Like mentioned previously, the pear channel is beta. What could be beta about it you ask? Well for one, the package and release plan that comes along with it. As of this writing, here is the plan:

  • ZF Package
    • Stable (no version modifier)
      • source: tag
      • schedule: on tag
    • RC – Release Candidate
      • source: tag
      • schedule: on tag
    • Beta (beta)
      • source: branch of current release branch
      • schedule: weekly
      • version: current + 1 mini
    • Alpha (alpha)
      • source: trunk
      • schedule: weekly
      • version: current + 1 minor
    • Development (devel)
      • source: trunk patched with selected incubator components
        • maintained in a file in incubator (locally for now)
      • schedule: weekly (or on demand)
      • version: current + 1 minor
  • ZF_Minimal Package
    • (scheme same as above)
    • Source modified
      • no tests
  • ZF_Extras Package
    • planning
  • ZF_Laboratory Package
    • planning
  • ZF_Doc_Lang Package (maybe)
    • planning

This might get tweaked over time, but the idea is pretty solid. Stable comes from tags as well as release candidate (and patch releases if they exist, not mentioned here). Betas are considered the next mini release, and alphas the next minor release. Development is super developmental, as you can see as its cut from trunk with selected incubator components.

More details will be forthcoming as I’m sure there will be questions you might have that are in search of answers. Till then…

Happy ZF-ing!

Slides for ZendCon Talks

September 17th, 2008 by Ralph Schindler

This blog entry will be a working page for those who would like to download my slides and code for my talks a #zendcon.

Zend_Tool Talk

New Zend Framework Quickstart Available

September 16th, 2008 by Ralph Schindler

With the release of Zend Framework version 1.6.1, comes a new Quickstart guide on the framework.zend.com website. This quickstart builds on all the great quickstart material provided by Aldemar Bernal, Bradley Holt, & Wil Sinclair on the wiki.

Over the past few weeks, myself and Matthew have gone back and forth on building a simple Guestbook application that would demonstrate what we think a 1.6 ZF application should look like if it were to be created from scratch.

As with most applications, most of the the structure this quickstart application we are suggesting is indeed subjective. We anticipate it will cause some discussions over architecture and best practices design, but then again, that is the point after all.

So, what does it highlight?

  • Zend_Controller & Zend_View – These elements are brought in from the original quickstart from the wiki. This includes building action controllers, views and also highlights error controller usage.
  • Zend_Config & Zend_Registry – Considering you will be building an application that will need to move from development to staging to production, we should have a config file that will support this process. Also, we will demonstrate the usage of an application registry.
  • Zend_Layout – Headers? Footers? Navigation? – That is where Zend_Layout shines when it comes to removing the common page elements from your view scripts. View scripts should be concise and only include information and logic about the action controller calling said view script.
  • And the biggie: Zend_Db_Table & The Model – In this simple guestbook example, we have built a simple Table Module based model for handling guestbook entries. For data access, this model will utilize Zend_Db_Table to access the guestbook entries table.

The quickstart is located at http://framework.zend.com/docs/quickstart/ and you can download the application (which is about 80% comments for your reading enjoyment), on the first page.

So, go download, read, build the application. Once you get a good handle on this quickstart, we will have a few addendums demonstrating new addons and features to this base quickstart application. Already planned is an addendum where I will walk through building this same model as a Domain Model using Zend’s Db_Table for data access.

Paying Homage to PHP4 – A Eulogy

August 8th, 2008 by Ralph Schindler

In technology years, I am an old man. I came to PHP in late 1998 while in college. Before that, I mostly developed in perl based on samples from the Perl Cookbook. I mean, where else were you suppose to start, right? Within a month of using PHP, it became abundantly clear that developers could become immediately productive by prototyping and building systems in PHP far faster than any other language I had encountered to date. And yeah, this was PHP3 – the other odd numbered release. If I recall correctly, the most difficult part of PHP at that time was making sure it would compile and install correctly on Redhat 5 and 6.

Any of these version numbers bringing back nostalgia to you developers?

Kind Words

The irony right now, is that most developers who have since embraced PHP5 are now treating PHP4 just as they would the death of a person that they disliked in life. For years (myself included), we’ve scoffed at PHP4 code. And why not? It’s so easy to spot. It generally looks as though it should come with a side of marinara and garlic toast. Most applications written on the platform started out clean, but then over time decomposed into a mess of tangled and indigestible code fragments. Revisiting these projects to add new features, or fix bugs (for the unluckiest of contractors) soon always began with a series of 4 letter expletives. It was these experiences that had us looking towards the future, the PHP5′s, PHP6′s, PHP10‘s.

So it has come, after years of cursing PHP4 by name, just as we would a villain in life, we assemble in this blogosphere to pay homage and eulogize PHP4 with the best of words, despite what he might have done in his old age. I’m sure the messy codebases of years past have not died with it, but at least there is a movement (much like a ball rolling down a hill, or an avalanche), to embrace PHP5′s OO model, unit testing, time tested software patterns and community accepted best practices.

PHP4: you slipped into a coma at the end of last year, and today (8-8-8) you died. I’ll recall some of the more interesting times I spent with you.

What did I do with PHP4?

The Ochsner Medical Center Wayfind and Directory Assistance Application

Around early 2003 I was contracted to build an application for a network of kiosks to display directory assistance and mapping at a relatively large hospital in New Orleans. Without going into all the massive details, this application took large jpg images with points mapping in a MySql database representing the hallways of the hospital on all 11 floors as well all all of the offices of all of the doctors. A flash application on the kiosk (which was touchscreen), would then ask the server for a specific doctor name. PHP would then (via all the great geometric math functions), would produce an XML file with all of the point to point animations that flash then had to do. PHP would also walk down all possible physical routes, going to all possible elevators trying to find the shortest route. It did this in real time. The database was updated often. It rocked.

Tulane Career Services Sign-in Computer

When I worked at the Tulane CSC, I was tasked with replacing our sign-in book with a computerized sign-in system. For this, we got a list of all the students from the Registrar, and bought a USB card scanner. Thankfully, there were some numbers in that little black strip that I could pull out of the Registrar database and mark the student as having visited the center in real time, without doing anything more than scanning their card. The card reader basically wrote to STDIN of the computer, so as long as the web text field had focus (enforced with JS), the system would work flawlessly– assuming the person with the card was in the system. If they weren’t they had to visit a web form, which sucks for them, but all in all, the system worked well.

Email Tracking System (Mostly for The Netcom Group & Starwood)

This was an interesting project. Given a list of conference attendees (sometimes 20 sometimes 5000): craft an email that had clickable and trackable links (that could get past spamassassin, hotmail filters, yahoo filters, aol filters, etc.), trackable open images that will redirect them to the conferences group rate hotel site for booking.

Each email was crafted based on that email list which was loaded into mysql by a php script. That php script then created and registered each individual link into the database so that reporting could be done later. PHP mail() was a workhorse. It got the job done, and we got loads of great information about email campaigns and tracking. Oh, and this what back when the likes of Doubleclick didn’t completely own the email campaign market.

The Flurry of Excel-a-like and custom CMS Applications

Of course there were clients who couldn’t find the off-the-shelf application to suit their needs. These clients generally wanted something that measured up to no more than a web based (CRUD) excel application with a few reporting features added in.

Projects like PEAR’s SpreadsheetExcelWriter greatly helped producing reports that would integrate nicely into a users desktop excel instance.

Like most PHP4 CMS applications, they were built by a developer or a group of developers, and generally, were built without the use of any application patterns. This basically meant that you had to get inside the head of the original developer to understand how the application worked. This made adopting any specific CMS just as expensive of a task as building a CMS from scratch that you could use over and over.

So this is what I did, as well as I’m sure 1000′s of other PHP developers over the past 10 years. This is why there are 100′s of PHP Application Frameworks.

Parting Words

PHP4, it been a long time since you’ve been installed on any of my servers, virtual machines, or desktops. And yes, I am glad you are gone, but also glad you gave birth to PHP5, and the PHP’s that will soon come after that.

PHP is dead. Long live PHP.

First Post

July 28th, 2008 by Ralph Schindler

For my friends that go back more than 5 years, you might remember I kept a blog at one point. At some point, I stopped having things to write about – so that blog/site got stale and eventually got taken offline.

Now I have things to write about! There will be a few tracks here, mostly Zend Framework and PHP related, but there will also be some posts on dining and food – and the occasional social rants ;)

Stay tuned!