__autoload() Fix for PHP4

Posted on


One of the greatest function ever added to PHP5 is __autoload(). It has been a very efficient help especially for an OOP-driven site. Through this function, there is no longer a need to include/require your class file every now and then. Once you have configured your __autoload() function, you are now good to go.

But what if you are using PHP4?

Just last week, I got devastated when I was informed that PHP version of my client was still in PHP4. My OOP coding was already in PHP5. I have to reconstruct my coding to adapt the server. So sad.

For some time I tried to think for a remedy. Luckily after some time, I found a solution.

in PHP 5

autoloadphp5

in PHP 4

This code works assuming that the filename is the same as the class name.

Just add the file checking using file_exists() function.

Hope this helps.

^_^

2 thoughts on “__autoload() Fix for PHP4

    Lysender said:
    May 18, 2009 at 9:44 AM

    Yeah, PHP4!

    tiago said:
    June 9, 2010 at 4:20 PM

    … and if i need to pass parameters to the constructor ?!!??!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s