Are You Using A PHP Framework Yet? You Should!
PHP continues to be a widely used web programming language as it’s one of the easiest languages to pick up, learn, and make beautiful things with. However, the reasons PHP is such an easy language allow for some absolutely terrible code to be written and put into production. Without some help, you’ll never be able to create applications on a large scale with a large team easily. You might think you can, but you can’t. Why? PHP on it’s own is very poorly structured, doesn’t enforce any sort of order, nor does it impose any discipline on you. Which is fine for small things, but can become a monstrosity of a codebase in a larger application. This is where PHP Frameworks come in.
A PHP framework, much like frameworks in other languages, is essentially a way to go about doing things in PHP. I don’t mean that a framework is some sort of guideline or document you should adhere to when convenient. A PHP framework is a layer on top of the actual ‘nuts and bolts’ PHP code that you interact with, bringing you, your code, and your team tons of benefits and time savers so you can focus on the features and conceptual aspects of your app without dealing with overhead that comes with doing basic things in PHP.
PHP frameworks are so robust today, that mastering them can take as much time as learning PHP itself. However, getting to know a particular framework is incredibly rewarding and will save you heaps of time. Let’s take a look at some of the benefits that a framework brings to the table.
MVC Pattern
All PHP frameworks that I’ve encountered use the MVC pattern, except for Zend (kinda). MVC stands for Model-View-Controller and is a completely different way of thinking about your application and it’s components. Traditionally, your PHP code in your head is viewed through files and individual pages.. Each file represents something, perhaps a page, or a class, or some other component.
With the MVC pattern, your code is broken up into three categories. A model will hold the meat of your application. For example, a blog-type application may have a post mode, a user model, and a comment, model.
Models are more or less what actually create, store, modify, retrieve, or manipulate data.
Views are just that. Different views of your application. In this blog-type application, we might have a view for the main blog page, single posts, comments, the administrator dashboard, a page to add or edit posts.
Views essentially are the frontend of your application, things like HTML, CSS, JS.
Immediately, you can see a benefit a framework provides here. Traditionally, your PHP files might have a mixed, ugly monstrosity all in one file. The one file might have code to call the database and retrieve posts at the top, then all the HTML at the bottom. This is a very messy way of doing this. The MVC pattern allows you to completely separate the business logic of your application from what the user actually sees. Bringing a nice zen to how you visualize and think about your code.
Finally we have the controller. The controller is what takes in user input such as, the URL that was requests, GET variables in the URL, and post fields. With this information, the controller will decide what model to use, send it data, get data back from the model, then decide what view to use and send it out to the user’s browser.
The MVC pattern alone is a great reason to start using a framework. It allows you to completely compartmentalize different aspects of your application into different models, views, and controllers. Now, each developer and each framework may have it’s own conventions about what exactly goes where, but MVC is MVC nonetheless.
It’s a framework, not a guide!
Using a PHP framework can save you incredible amounts of time as a framework is just that. It’s essentially a ready to go application out of the box. It may not do anything useful, but a framework out of the box is actually a complete application that you can run in a web browser.
Why is this important? Realizing that the framework is a full application will give you the right mentality and see that you are adding your features and what you see as your application on top of the framework. This means there are tons of tools the framework already makes available to you. Things like connecting to and accessing database information easily without you having to create or use a separate database class or function, some frameworks will have features allowing you to easily secure and sanitize input data. Most will even have features allow you to create incredibly robust forms.
These are just minor examples, the point is that so much time and work is already done for you. In a traditional application, you might have to spend time and write out functions and classes to get rich database connectivity. With a framework, you just use what’s already there!
Faster Development
Once you pick a framework, learn it, and use it more and more, you’ll realize that you get things done much quicker because you’re changing how you think about your application application. You go from seeing it as a PHP application to seeing it as a [INSERT FRAMEWORK NAME HERE] application. Most likely the framework you’re using is designed to allow for much more rapid development than using PHP directly.
Here’s a simple example of how easy it is to get information out of a database using the Laravel framework. Let’s say I have a table in my database called users, here’s all it takes for me to get information from that table.
First I create a User model.
class User extends Eloquent {}
Then in my views or controller, and I can do cool stuff like this!
$users = User::all(); # The $user variable will hold an array of objects with all the users. $sajan = User::find(4); # The $sajan variable will hold an object of the entire row where the id = 4. # Similar to the following SQL query: SELECT * FROM users WHERE id = 4 $user = User::where('email', '=', 'email@sajanp.com')->first(); # This is similar to a SQL query like: SELECT * FROM users WHERE email = "email@sajanp.com" LIMIT 1
Of course you can get much more fancier than that and do some really dynamic things. However, you see how expressive that was? I didn’t have to off looking for some old code snippet or type ugly code to do that.
How Do I Get Started?
First thing I’d like to point out is that since most PHP frameworks are such a thick layer on top of PHP, it’s a good idea to be very familiar with PHP itself before diving into a framework. Many believe that using a framework might be a shortcut to mastering PHP. Wrong. You will only find yourself lost as the use of frameworks do assume a strong grasp of the underlying concepts.
Obviously the first thing you want to do is pick a PHP framework to use. As mentioned before, learning a framework will require the same effort as learning PHP itself. So it’s best that you be ready to invest some time in things and look around before you choose one you really want to go with.
While there are tons of really solid PHP frameworks out there, here’s a list of some of the bigger names that you can check out for yourself. Most are catered to a very specific type of developer and have different featuresets. So make sure you shop around and find the one you like to work with the best.
- CakePHP
- CodeIgniter
- Symphony
- Yii
- Zend Framework
- Laravel
I personally love Laravel. It’s one of the most beautiful and fastest frameworks I’ve used.