The Awesomeness of PHP __autoload

Many of you have probably written or at least tried to write a PHP framework at some point during your career. The best way to write a frameworks, in my very humble opinion, is to use object oriented programming (OOP) and a sort of model/view/controller (MVC) architecture. In most cases this means that by the end of writing a sort of usable system you will have a large amount of files filled with different bits of code.

In the bad old days before PHP 5 you would have an index.php file and that looked something like this:

<?php
/**
 * Index file of Xframework.
 */

// Include all sorts of useful stuff...
include_once '/tag/php/bootstrap.html';
include_once '/app/model/x.class.html';
include_once '/app/model/theme.class.html';
include_once '/app/controller/user.class.html';
include_once '/app/view/index.html';

// Now we can finally do something.
$x = new x();
$x->run();

Wouldn’t it be really nice if we could just write the code without having to worry about including millions of files everywhere, which can become a pain in terms of maintainability.

Enter __autoload() in PHP5. YAY! All our troubles magically disappear. Well not really, but your life is made slightly easier. So instead of the above, you now have:

<?php
/**
 * Improved index file of Xframework.
 */

// Our pretty __autoload() function.
function __autoload($classname) {
  include_once '/app/classes/'. $classname .'/tag/php/.html';
}

// Run the framework.
$x = new x_controller();
$x->run();

Using __autoload should theoretically also improve the speed at which your app loads because you only load the files that are needed at the time.

You could also do some really complex configurations like the below for example:

<?php
/**
 * A more complex __autoload example for Xframework.
 */

// Autoload some classes.
function __autoload($classname) {
  $class = explode('_'. $classname);

  $class_file = $class[0] .'/tag/php/.html';
  $class_type = $class[1];

  // Include the class.
  include_once '/app/'. $class_type .'/index.html'. #class_file;
}

// Let's run the app.
$x = new x_controller();
$x->run();

In the example above the autoload function is checking the class name for a type. The type could be, for example, model, view, controller, elephant or whatever you wanted it to be. That type is the directory where the class is stored. In the example we called the x_controller class. So that class would be located at /app/controller/x.php, the same would be true if you wanted it to be x_model or x_view.

This most definitely makes my life easier when I write PHP applications. If you spend a lot of time writing PHP applications and don’t use a specific framework for the job I would suggest that you put together a rough toolkit for yourself using __autoload() as the heart.

SQLite3 + Dropbox == Awesome

Dropbox is one of the coolest apps I have ever used and everyone knows how to use it. I can tell my clients to Dropbox me files and they don’t roll their eyes. Finally something useful out there. No, this isn’t an advert for Dropbox I will explain my excitement in a minute.

I wrote a PHP/MySQL contact manager for a client a little while ago, it was the successor to an Excel VBA app that succeeded a Psion Series 3 from 1993. So the client was very happy with the system everything working perfectly. But then he wants to be able to access his database of contacts from other computers as well (work / home / etc…). Initially I was going to build an addon that allowed him to export a file and import it on the other end. But I didn’t really like that idea because it relies too much on the client to get it right, which is not a good thing at all. (Think late night phone calls and weekends filled with a moaning client)

It took a while to realise that perhaps Dropbox could do exactly what I wanted, the thing that held me up was MySQL. Enter SQLite3. I have know about SQLite3 for a while now but I have never used it much. I knew it was a portable database that lived in a single file but have never had a use for it. Until now.

PHP also makes it really simple to convert from a MySQL database to a SQLite3 database, thought I did spend a lot of time fiddling with export tools. I ended up porting the table structure and then writing a simple MySQL query that pushed all the data into the SQLite3 database. It took me around two hours to convert the contact manager from MySQL to SQLite3 which is not bad. All the queries work without needing to change, basically you just change any mysql_* function to sqlite_* and hey presto you’re done :) .

And for the cherry on top I use Dropbox to store the SQLite3 database and then reference it from the PHP application running in XAMPP on his local machines. Then whenever he updates his database on one computer it updates them all. Also with Dropbox he can work offline as well and then sync when he connects.

Of course I know this is not the elixir to everything but for something simple like this where there is one guy using an app on several computers it is perfect.

I think maybe for my next project I will start working on building a Dropbox clone to use in my own office, maybe with a few extra features more useful to developers. Haha, no… but then again…

PHP SSH2 on Fedora Core the easy way

Installing the PHP SSH2 extension on Fedora is a pain in the backside unless you know how. There are countless examples for Ubuntu but everyone seems to leave the Red Hat family in the dark.

Here is the easy way to do it:

[theamoeba@amoeba2]# yum install php-pecl-ssh2
[theamoeba@amoeba2]# service httpd restart