A simple PHP 5 __autoload() example
This is probably the one reason why everyone should switch to PHP 5! According to Charl it is “teh shit”. It is the __autoload() function. You define an __autoload() function and by calling this function the scripting engine is given a last chance to load the class or include the class file before PHP fails with an error.
I’ve created this very simple example to demostrate the effectiveness of the __autoload() function. In the index.php file, I define my __autoload() function and tells it to include a file with the same name as the class. In other words, my class name is oneClass() so the file which contain oneClass() is called oneclass.php
//The magical autoload function...
function __autoload($classname)
{
require_once $classname . '.php';
}
Now I initialize class two :
$class2 = new twoClass('Second class');
$class2->getText();
PHP will require and include the file called twoclass.php without me doing anything. Nothing funny here… Thing is within class two I initialize class one. As you can see, nowhere did I say it should be included! So withing class two it autoloads class one and initialize it.
class twoClass
{
private $text;
function __construct($txt)
{
$this->text = $txt;
}
function getText()
{
$class = new oneClass($this->text);
$class->getText();
}
}
w00t!!! Nifty, hey? Note that I’ve create the page second.php to demonstrate how the same would result in an error due to the fact that I did not require class one. If you’d like to check it in action, feel free to download this example here.
PHP Stii
PHP confusion - why do some software still insist on PHP4?
PHP 4 reached its end of life the 31st December 2007. They will continue to fix bugs up until 08 August 2008. Yet, most of the biggest CMS software, guys like Joomla!, Drupal and Wordpress keep on supporting PHP 4 instead of PHP 5. Why on earth?! I do get it that you cannot predict what version of PHP would be on a shared host, but the hosting providers should pull up their socks and upgrade!
PHP 5 is a 1000% improvement on 4 and has been out for a couple of years, so why would some shared hosts still cling to PHP 4? If these guys like Joomla!, Drupal and Wordpress develop in PHP 5, surely hosting companies will be forced to upgrade their PHP version? It is not like they would suddenly break all PHP 4 code as 5 is perfectly backward compatible. So what is the issue? When will this software be rewritten in PHP 5? Hehehe, that would be quite a feat, except that Joomla! 1.5 was written almost from the ground up not all that long ago!
PHP, Programming Stii





















