Wednesday, December 07, 2005

PHP Applications with Smarty and OOP

Introduction

With PHP script kiddies (including myself) around every corner, it's time we start distinguishing ourselves from the crowd and start writing more than just web scripts. Yes, you read right, PHP Applications. Entire Applications consisting not of multiple scripts, but of multiple objects, templates, and possibly even a solid framework!

You may ask why use PHP for an application at all, to which I wouldn't reply, because this is a blog, not a forum. Stop talking to your monitor. Seriously... it's kind of creepy.

The Tools
PHP 5
While PHP 4 is perfectly capable of creating great online web applications, so is PHP 5, and PHP 5 has the ability to create fully-functional Objects with Public and Private variables and methods.
The number one technique that has changed the way I write applications is the practice of extending classes. When you extend a class, you are creating your own new class that has all sorts of custom methods, but also has full use of the original class that you are extending. An example of when to do this would be if you had a class that you used for various portions of the application, but needed added functionality for a different area of the application, or for a different application altogether.
I'll go more into the benefits and techniques in a tutorial later on the main WovenThorns.com site, but for now, let's just assume PHP 5 is a great tool that has been around for over a year and is more than stable enough for production use.

Apache 2
Apache 1 vs. Apache 2 doesn't make a whole lot of difference to me, other than the administration side of things. Keeping track of multiple sites is much easier, and there are more modules being actively worked on for Apache 2. Once again, you can use IIS on a windows machine instead, but why would you want to?
The number one technique in Apache 2 that I love to take advantage of is mod_rewrite, which allows you to rewrite URL's, such as http://www.example.com/articles.php?id=5&page=3 to http://www.example.com/articles/5/3 (which looks much nicer and is noticed more in search engines)

PEAR DB
If you don't know what PEAR is, head over to http://pear.php.net and find out. While it is not neccessary for all applications, they sure do have a lot of great tools, such as PEAR DB that make writing web applications a whole heck of a lot easier, and make your tools much more portable. PEAR DB is a catch-all database class, that allows you to query a database without having to worry about the syntax for a specific database server. You just have to know the proper connection string to use with PEAR DB, then it will use the appropriate PHP tools to connect to your database, whether it is MySQL, PostgreSQL, Oracle, etc. This means you can write an entire PHP application without hard-coding a specific database vendor. If you decide to change Database Vendors from Oracle to PostgreSQL, then you only need to change the connection string, and the rest of your application will remain functional!

Smarty
Smarty is a great templating class written in PHP that makes creating web applications very organized and easy to modify. Smarty makes it simple to separate code from design, allowing your design team to work on layout and graphics and freeing up your development team to write the code without worrying about layout.
If you are unfamiliar with Smarty, head over to http://smarty.php.net and check it out. They have a great tutorial on creating a web application with the techniques hinted on in this article as well.

Conclusion
Don't re-invent the wheel, and don't program yourself into a corner. Keep things modular with PHP5 Object Oriented Programming techniques, separate your database from your application with PEAR DB, and separate design from code with Smarty.

Look for tutorials and help forums when the new and improved Woven Thorns Productions is released later this month!