Mohamed Yousef
Mohamed Yousef

Reputation: 41

Fatal error: Constructor Autoload::autoload() cannot be static in C:\xampp\htdocs\project\autoload.php on line 3

That is my code that giving me the error

autoload.php

<?php
class  Autoload{
    public static function autoload($className){
        $className = strtolower($className);
        require_once $className.".php";
    }
}
spl_autoload_register("Autoload::autoload");
?>

But when I'm using namespace it work good

autoload.php

<?php
namespace Project;
class  Autoload{
    public static function autoload($className){
        $className = strtolower($className);
        require_once $className.".php";
    }
}
spl_autoload_register(__NAMESPACE__."\Autoload::autoload");
?>

I want to use my script without namespace. All script files are in the same root

Upvotes: 1

Views: 262

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53563

Your method name matches your class name -- this is PHP's old style of defining a constructor. Just change the name of either the class or the method so that they don't match.

class Autoload
{
    public static function load($className) {
        $className = strtolower($className);
        require_once $className.".php";
    }
}
spl_autoload_register("Autoload::load");

Upvotes: 2

Related Questions