Auto Loading Static Classes Does Not Work

Hey guys I need some help.

I have this code:


function __autoload($class)
{
	$file = $_SERVER['DOCUMENT_ROOT'].'/_global/classes/'.$class.'.php';
	if (file_exists($file))
	{
		require_once($file);
	} else {
		return false;
	}
}

It works fine when I call a class like this:
$database = new Database();

but when I try to call a static class like:
Error::test(‘test’)

It will not auto load the class. Does anyone know a solution to this or am I going to have to manually include my static classes?

Thanks,
Darren

Works for me :smiley:

I have added an echo statement


echo 'autoload ' . $file . ' <br>';

which confirms that the attempt is made.

autoload C:/wamp/www/_global/classes/Error.php


<?php
function __autoload($class) {
    $file = $_SERVER['DOCUMENT_ROOT'].'/_global/classes/'.$class.'.php'; 
    echo 'autoload ' . $file . ' <br>';
    if (file_exists($file)) 
    { 
        require_once($file); 
    } else { 
        return false; 
    } 
}
Error::test('test');
?>

I haven’t had a chance to read the documentation on how autoload handles static methods but my guess would be the following:

It could be that it requires you to instantiate an object.

$database = new database() works in this case.

But Error::test() does not.

So with that logic:


class Error {
  private $instance; // holds the instance of the object

  public static function getInstance() {
    if (self::$instance instanceof self)
      return self::$instance;
    else
      return self::$instance = new self();
  }

  private function __construct() { }

  public static function Test($word) {
    echo $word;
  }
}

$error = Error::getInstance();

$error::Test(‘Hey!’);

PS

My post became irrelevant when pmw57 posted. :blush: It might be that you’re doing something wrong. Without further code, it’s hard to tell what’s going on.

Cerium I just made it a normal class. Thanks though guys.