HomePHP An Introduction to Simulating the Model-View-Controller Schema in PHP
An Introduction to Simulating the Model-View-Controller Schema in PHP
Would you like to learn how to simulate an MVC-based system with PHP? If so, you've come to the right place. This is the first article in a three-part series that will show you how to build this schema in PHP by constructing a few classes that represent what is needed.
When it comes to talking about the good points of a server-side scripting language like PHP, indeed what comes to my mind is its easy learning curve, or its great versatility for interacting with different RDBMS, and of course its excellent implementation of the object model. This last feature is particularly powerful in PHP 5. In this case, I might go on and on listing all the handy features of PHP, but quite possibly you (and me) would get bored quickly, and this wouldn't be an eye-catching subject for an article.
However, in order to make things more interesting, let me stand on the other side of this terrain for a moment, and speak about some of the most relevant of PHP's drawbacks. It's a fact widely known that its natural ease can lead sometimes to the development of dirty code, or that some of its object-oriented characteristics aren't as thought out as the ones present in other languages like Java or C++. True to form, for the well-being of many PHP developers, most of these limitations can be tackled with a good amount of experience and willpower.
Now, and specifically speaking, among the features that PHP lacks, there's one that has very popular nowadays: the implementation of a strong Model-View-Controller schema, a concept that has been taken to a superior level with languages like Ruby (and obviously its framework Rails).
If this doesn't ring any bells to you, by implementing a Model-View-Controller relationship, you can have a programmatic model, for example a database, which is handled by a set of classes (the controller), all of them being capable of instructing the respective model to generate different outputs (called Views). This paradigm can be as simple as working with three separated entities, or as complex as using multiple databases, generating complete directory structures on the fly, and building up full-featured data grids.
Well, you may say that such a thing isn't necessary at all for being included in your PHP applications, and certainly you can live happily without it. However, as I said before, it's possible to simulate a MVC schema with PHP, by constructing a few understandable classes that represent a Model, a View, and lastly a Controller.
However, before I introduce the subject in question, Iíd like to clarify one point that I think itís important to stress: the example that Iím going to develop here is only a basic introduction to the MVC pattern, which means that the corresponding independencies between the sample classes wonít be following a strict model.
Okay, having clarified the above discussed point, the only thing that I ask from you is a little bit of your willpower, thus you can understand easily how a simple MVC schema can be developed with PHP. Assuming that you're already prepared, let's get started!