Modern Design in PHP

Posted by acts_as_flinn Sun, 19 Aug 2007 04:57:00 GMT

Yesterday I finished my first week at my new job. Going back to the PHP world is somewhat shocking to see a complete lack standardized coding methods. I spent some time this week getting to know the project I’ll be helping out on for the next several weeks. The code originates from an overseas outsourcing company with absolutely no standards or knowledge of design patterns, or concept of writing DRY code.

I find it difficult sometimes to get the concepts I’ve picked up during Ruby development across to PHP programmers. Since most people self-learn PHP there is a tendency to do a lot of brute force coding, copying and pasting and doing stupid stuff like using template languages when there is know need to use a templates. Because they self learn, modern software engineering philosophies and management techniques that are progressing the state of the art are virtually unheard of by many PHP developers. Let’s explore some of the things PHP developers could be doing to make their lives better, save their employers money and maybe get a raise.

Views Instead of Templates

In the PHP world there is this mysterious draw toward templating using engines like Smarty, Flexy (which I have been guilty of), Blitz or any of the other engines available. I think this draw is the experience of the developer wanting to abstract and separate the presentation layer. It’s a natural progression that comes from experience – the kind of experience you get from PHP’s free style – so free that you often see SQL statements, and HTML markup embedded within the same PHP script.

So why use a template engine in the first place? PHP is a template engine. The only valid reason I have ever found is for applications that allow either designers or end users change the front end presentation layer – and even then I’m skeptical what with the whole existence of CSS…but I can understand the level of flexibility templates allow a designer. A good example that I have used is Shopify use of the Liquid Markup Language which was created for Shopify. Anyway, back to the subject.

If there is no reason for a third party designer to change the presentation layer there is almost no need for a template language, but you see templating in PHP frequently used as a way to force code separation.

So PHP coders pay attention… Forget the template engines unless you really need them they only indirectly force you to do code separation, slow down your application, learn a bunch of additional syntax and limit your ability to present what you want to in PHP. Just chose to do code separation from the design phase of your application. Keep the power of PHP available in your presentation layer views and skip learning a pointless template language.

Testing, Testing, 1. 2. 1. 2.

How do we test in PHP? Write a script, hit the browser and look all over the place to see if what you wrote worked). WRONG Well not really – most PHP developers do just that. No automated unit testing or integration testing, and most have never heard of it. I’ll be the first to admit that when I made the leap from PHP to Ruby I had never heard of unit testing and didn’t incorporate testing until after at least a year of Rails development. I’m also the biggest advocate for automated testing.

People often resist writing automated tests because they can be time intensive at the onset of a project. It’s often a matter of “we don’t have enough time or funds to spend writing tests.” The proponents of this argument are usually the ones in favor of brute force eye ball tests. “Let’s make a change and cross our fingers hoping it doesn’t break anything we’ve already eyeballed.”

Well that just doesn’t make any sense! It’s a business decision that budget managers should understand. Why spend more time eyeballing and crossing your fingers when PHP has unit tests, continuous integration tests and a other great methods. Managers should be asking their developers why they aren’t using PHP’s automated testing facilities!

Data Modeling and Class Design

I do my little turn on the cat walk, yeah on the cat walk… Modeling seems to be an esoteric subject for some PHP developers. In a lot of database schemas I repeatedly see weird table layouts, aggregate data storage and lots of abbreviations. In Ruby on Rails the convention is to model tables based on object usage. We use the Active Record design pattern. PHP developers often think ActiveRecord and Active Record are one and the same – they’re not.

Active Record the design pattern is a description for creating classes that relate to a row in a database, provide static finder methods and implement an object that both provides an interface to data and the methods to store, destroy and modify the data. The pattern is impossible to simply implement in PHP. But that doesn’t mean it can’t be used to the fullest of PHP’s ability or that you can’t implement the next best data design pattern.

Why use abbreviations in database tables? Is it really that convenient to abreviate the hell out of every variable or attribute name? One of the best things you get in Rails is string inflection (huh?). That means we can take a string like “phone_number” and call a method like humanize resulting in a human readable field label, “Phone Number” – using this is a no brainer. It also makes it a lot less tempting to use variable names like “phonum”...but that’s really a no brainer too.

What looks better to the human eye? “phonum” or “phone_number” – you can see where this is going… ”$phonum”, ”$cli_phonum”, ”$client_phonum”, ”$client_phone_number”, ”$client->phone_number”. So if you were working in a team what convention works best, esoteric abbreviations or human readble variable names that any programmer can jump into without a secret decoder ring.

If you’ve got a form full of aggregate information keep it separate. This is an easy one – when you’re on the receiving end of a post why reassemble your information?

HTML


<input type="text" name="client_name" id="client_name" value="Joe Smalltime &amp; Son" />
<input type="text" name="client_email" id="client_email" value="" />
<input type="text" name="client_login" id="client_login" value="" />
...

PHP


$client = array();
$client['name'] = $_POST['client_name'];
$client['email'] = $_POST['client_email'];
$client['login'] = $_POST['client_login'];
...

Or do nothing..

HTML


<input type="text" name="client[name]" id="client_name" value="Fortune 500 Corp." />
<input type="text" name="client[email]" id="client_email" value="" />
<input type="text" name="client[login]" id="client_login" value="" />
...

PHP


$client = $_POST['client'];

Do what you mean, and only that.

Why make things more complex than they need to be? If you mean to take information from the web and save it, do just that. Where should we validate information, every script that needs to accept and save information or in the one place it actually matters? Why validate manually in every script when we can set our validation conditions at the class level then do what we mean? What am I talking about?


if (valid_client_info($_POST['client_info']) {
  save_client_info($_POST['client_info']);
} else {
  throw new Exception("Your client info sucks");
}

Or we could do this…


  $client = new Client($_POST['client']);
  if ($client->save()) {
    $this->render_string("Hazaa!");
  } else {
    throw new Exception("argh!");
  }

Conclusion

PHP developers can learn a lot from Ruby on Rails. You really don’t have to use Ruby to take advantage of modern design techniques but what a lot of people fail to realize about Rails is the fact that most of the conventions weren’t designed just for ruby – Rails is a laundry list of best practices for web development. Some of the best developers in the world are using Rails because of it’s best practices that enable rapid development and get you focused on the business logic not the repetative tasks you face every project.

Trackbacks

Use the following link to trackback from your own site:
http://www.actsasflinn.com/trackbacks?article_id=modern-design-in-php&day=19&month=08&year=2007


ss_blog_claim=746d258dc975cb7923cc57154dbf1d71