Updated every Monday, Wednesday and Friday
27Jul2009

PHP: MVC By Nature?

After spending hours upon hours fighting with frameworks, I got fed up. They are certainly tempting, since they take care of the initial structuring of your program. However, once you get past that- what do they bring to the table? Are they really worth the inevitable aggravation?

Some people will have thought about this thoroughly, and come to a different conclusion than me. Others will have just a gut reaction to me saying that procedural programming still has a place in PHP. Either way, a lot of people are going to disagree.

So, with that, here goes my argument that PHP is already MVC-ready.

Views

PHP is unique from many compiled, desktop-centric languages in that all presentation is done using a separate language (a markup language, but a language none the less). Short of XML output and the like, all PHP needs to be echo‘d and print‘d out among longhand HTML. Even in my earliest, blissfully-ignorant-to-MVC days, it was apparent that it made more sense to separate the HTML from the PHP. (Of course, there is a subtle yet distinct difference between separating the PHP from the HTML, and the logic from the presentation- but remember, I was still MVC-ignorant back then). A simple include does the job. So, why do we need a framework to do this for us?

Of course, you could argue the merits or disadvantages of using a templating engine like Smarty, but the point is moot- none of the major PHP frameworks really have any templating features beyond what an include can do.

Models

Models are covered by objects- they take care of the data. More often than not, the job of the model is to convert raw data (often from a database) into something the controller can work with. Models take care of the tedious business logic, creating a bridge between the data and the code. Writing out SQL statements in the controller quickly gets messy- models make it easy to keep the data all in sync, across the entire program.

One thing that has always bugged me is that in frameworks, you have to load a model before you can use it. Why? With PHP, you can automate this- going through a framework just makes debugging even harder. That’s not to say that extending a generic Model class can’t help your models- however, we don’t need a full framework for that.

Controller

With me so far? Well, here is where I will probably lose you.

First, let’s make something clear. I can’t say enough good things about OOP- so I’m not even going to bother trying. Barring “Hello World,” I can’t think of a single program that would not benefit from OOP.

However, here’s a quote from Rasmus Lerdorf (the creator of PHP):

PHP has traditionally been a procedural language, and OOP features have crept in over the years to the point where PHP can be used as a decent OOP language.

I never understood why we are so fond of using OOP for the controller portion of MVC. I understand the arguments for it- however, given the bland, generic way frameworks currently utilize OOP in controllers, it seems it is used more for the sake of being object oriented than because it is the right tool for the job.

How about this for an alternative: One index.php file, that takes care of routing. It can be more flexible, in my opinion, than the regex-based routing implementations in most frameworks. Yes, I do agree that programming is easier when we can look at a URL and instantly know the class and method the code is in. However, I also feel there’s merit in dropping the “automagic” that happens when a framework handles our routing. (And honestly, I’ve never worked on a framework based project that had a URL structure that mapped directly to the class-defined structure anyway.)

If we do it this way, we can move the structure from the classes to actual folders. We can split the PHP up among a bunch of smaller, procedural files. On top of that, if we commit to mirroring the URL structure, we don’t lose anything in the way of navigating code.

Libraries and Helpers

In the spirit of not reusing code, we can still make liberal use of libraries and helpers- basically, classes or functions that handle any code we may need to use more than once. These would have to be include‘d on a use-by-use basis, of course- but how hard is that?

In my opinion, the biggest problem with frameworks is they’re not very good at letting you do things your way. For a team of inexperienced or lazy programmers? This is great, as it makes sure everyone is on the same page and does things the “right” way. However, more often than not, I find it annoying that I’m suck doing things the frameworks’ way.

JavaScript framework/libraries have it right- you can use them as much as you want, but if for whatever reason you want to use straight JavaScript? They stay out of your way. Why can’t PHP do it this way? We should worth with PHP libraries, not frameworks.

The Benefits

  • Libraries no longer have to be framework-specific. This opens us up to a lot more prewritten code that we can utilize.
  • Faster. No matter how fast frameworks are, they’re still slower than straight PHP.
  • Easier to follow the code, if you don’t have to loop in and out of foreign code to debug it. Using a frameworks loader will often break most PHP IDEs and debuggers.
  • Less things that can go wrong. If something breaks with just PHP, it’s either PHP or you (and, more than likely, it’s you). If you are using a framework, there is a third party that could also be at fault.

Conclusion

Model-View-Controller architectures and Object Oriented programming are fundamental to making intelligent, easy to read applications in PHP. When we start a program, we need to take the time to think about how we can structure the code in a comprehensible way. However, we don’t necessarily need an MVC framework in order to keep our program separated into models, views and controllers. PHP is already MVC-ready- so rather than focus on frameworks (which, in my opinion, tend to be lacking), we should create our own well-thought-out architectures and use libraries for the code we want to outsource to open source.

Sure, it’s not as glamorous or trendy as an MVC PHP framework. But at least one person agrees with me.

http://www.blog.gkoberger.net/2009/07/13/some-codeigniter-gotchas/
in Programming — by Gregory
22Jul2009

A Solution To E-Mail Validation

Whenever it comes time to validate an email address, I always end up going through the same exact process.

I start with a simple regular expression- for example, this:

(.+)@[\w]+\.[a-zA-Z]+

It works great for a majority of email addresses- however, it’s not perfect. It lets through a lot of invalid email addresses. For example, “.name@something.a” is considered a valid email address by this regular expression. So, let’s take a few steps to correct this:

([-a-zA-Z0-9])([-.a-zA-Z0-9])*@[\w]+\.[a-zA-Z]{2,5}

A bit better, right? But what about valid email addresses, such as john+smith@mail.something.com and $A12345@example.mobile? Same goes for the seemingly invalid email addresses such as “Abc\@def”@example.com. But, alas, these are all valid.

in Programming, Usability — by Gregory
20Jul2009

What Kind of Company Are You?

The biggest problem inside companies is self interest. The sales team wants to make money, the editorial team wants integrity, the legal team wants to avoid lawsuits, the engineering team wants to use cool technology- the list goes on and on.

Does every company need a little bit of everything? Of course. However, it’s important for companies to decide which is the most important for them. Otherwise, every department is fighting for the spotlight- and they all end up being at odds with each other.

A company needs to pick that one attribute it wants to strive for- and if it’s done well enough, the other qualities will come naturally. And it’s important it only goes for one- trying for numerous of these attributes from the start will result in a company with an unclear direction, which is apparent to consumers.

in Uncategorized — by Gregory
15Jul2009

I’ve Been Had!

After ten years of using the same password, using strange programs and logging in on public computers, it finally happened- someone broke into all my accounts.

It was a long time coming- I started to get a bit cocky about my online security, after all. Same passwords everywhere, no spyware programs running, no encryption anywhere. But hey- ten years without anything going wrong? You let your guard down.

in Uncategorized — by Gregory
13Jul2009

Some CodeIgniter Gotcha’s

Maybe I’m doing something wrong. In fact, for the sake of programming, I hope I am. But for the life of me, I can’t find a single benefit of frameworks that isn’t overshadowed by the negatives.

Frameworks make it easy to start out- meaning that, when facing a new project, they seem like a good idea. You don’t have to do all the boring stuff at first- and when you’re starting a new project, that’s a tempting proposition.

This topic deserves a much more thorough analysis (which many before me have done, and I’m sure I someday will), however after spending the weekend struggling with CodeIgniter, I figured I’d mention some CodeIgniter-specific problems I’ve come across.

in Programming — by Gregory
10Jul2009

The Portfolio Rule

I have a simple rule when it comes to deciding if a design is finished: would I put it in my portfolio?

I used to be pretty good at convincing myself my latest work was “good enough”- deadlines, boredom and difficulty definitely encourage such rationalization. I used to ignore that voice in the back of my mind that said it just wasn’t there yet. Since I started trusting my gut instinct, I’ve found myself asking “what was I thinking?” a lot less often.

Everyone will tell you “bad isn’t the enemy of good, perfection is”- and that’s definitely true. But all to often, it’s used as a cop-out. An excuse to release something that’s not quite up to par.

So, next time you’re wondering if you’re done- is it good enough to land you your next gig? Are proud enough to put it in your portfolio? And, of course- remember to be honest.

in Design — by Gregory
8Jul2009

Masking Passwords?

Recently, usability expert Jakob Nielsen made the case that we need to stop password masking. After a few days of mulling over the suggestion, here is what I came up with.

Who’s Job Is It?

In his post, Nielsen implied that the unmasking of passwords should be put into effect by the web developers. However, his suggestions would work better if implemented at the browser level. This way, users could easily and globally toggle the feature on and off, and decide for themselves if they want it. (And, of course, there’s already a Firefox plugin that makes the whole argument moot- users who prefer this functionality can already choose to use it.)

Usability Concerns

Consistency is one of the most basic usability principles. If users know that the same command or the same action will always have the same effect, they will feel more confident in using the system[...]

-Usability Engineering by Jakob Nielsen, p 132

Nielsen himself claims that consistency is important- so how can users feel confident, if some password boxes protect their passwords while others do not? Not only is this an argument for it being a feature in browsers rather than websites, it’s an argument why it’s dangerous for web developers to even attempt it. It would be one thing if all passwords were unmasked- however, Nielsen is calling for it’s implementation on a site-by-site basis.

Breaks Browers

By removing the type=”password” from the input element, we make it so that browsers won’t be able to tell the submitted field is a password- effectively breaking the “Save password” feature that saves us from having to type in passwords constantly. And, even if this didn’t break the feature- would you want your saved password popping up in plain text every time you hit a site with a login box? (Of course, saved passwords could be shown masked- but this is again a browser-level feature, as well as defeating the purpose of plain text passwords.)

Perceived Security

It’s true that masking makes no no difference in real security, however Nielsen has completely ignored perceived security. Users don’t understand that the only thing masked passwords protect them from are people physically standing behind them- having their password show up in plain text will no doubt result in many users believing anyone anywhere can nab it.

Icon

As made obvious by the press this suggestion has gotten, people listen when Jakob Nielsen talks. If he was serious about people implementing this, he should have attached some sort of pseudo-standards to his post. Had he created small masked/unmasked icons, he could have done for this what the little orange icon did for RSS. People could have seen the icon, and known right away that the password field was unmasked (and known that clicking the icon would turn masking on). Rather, anyone who implements it now has to individually explain to the users what’s going on, and how to toggle it.

Testing

One thing that always impressed me about UseIt.com was the research that went into every alert. He always seems to have statistics to back up everything he said. In this post, there was none. Had he found 20 people with various degrees of computer know-how, asked them to register for a mock site, and recorded their reactions? Then we could be confident one way or another. He’s forgotten that one of the basic principles of the field of usability is that even the experts can’t be confident until they observe actual users.

My Conclusion

We have to forgive him a little, though- his perspective is from that of usability only, and nobody can disagree that it is easier to type a password when you can see it. That being said, I would argue that his view of this was too narrow; if he did indeed look at it from a broader context, he didn’t let it show in his post. Even from a usability standpoint, it’s easy to see how this could be a potential problem. He answered a very black-and-white question- “Is it easier to type when you can see what you’re typing?”- and the answer is, of course, yes. While he does spend a paragraph conceding, almost reluctantly, that “sometimes security should win,” overall he stays away from exploring the ramifications of removing password masking- writing it off as Geocities-era “legacy design.”

He is right about one thing, though- those reset buttons need to go.

in Usability — by Gregory
6Jul2009

The Medium is the Message

When Marshall McLuhan wrote his famous phrase, “the medium is the message,” he was talking about about TVs, newspapers and radios. However, his insights into how the medium influences how we perceive the message can just as easily be applied to modern day RSS feeds.

When running a website, a lot of work goes into creating an atmosphere. The colors, fonts and layout of elements are carefully chosen to convey a certain tone to the reader. When we use RSS, we strip out everything but just the words. While no doubt an extreme example, it’s a bit like reading just the manuscript for a movie.

There are a lot of good things to be said about RSS- it’s an incredible example of how web standards can lead to innovation, and it can keep us from having to refresh our favorite websites constantly. However, when we rip the text from the site, we lose a lot of visual cues- everything from quality to credibility can be gleaned from just looking at a layout. The design of a website is just as important as the content- and often exceeds it.

So, as a blogger, how can you get people to go to your site rather than subscribe? Try adding features that can’t be reproduced using RSS- Digg has an RSS feed, however I would wager that most people go straight to the site. Or, try posting on a rigid schedule (and keeping it!). Users will know exactly when to check your site to get fresh content (and don’t ever add “extra”, unscheduled posts- it may seem like a treat for your users, however it defeats the whole purpose).

in Design — by Gregory

Website by Gregory Koberger
518.339.4652 | gkoberger@gmail.com
gkoberger.net | blog | about gregory