5

here to ask is any error for my autoloader class/function with spl_autoload_register?

appreciate for helping.

here is my code

<?php

namespace system\core;

// if(!defined('IN_APP')){
    // exit('Access Denied');
// }

define('ROOT', $_SERVER['DOCUMENT_ROOT']);
define('DS', DIRECTORY_SEPARATOR);

if(function_exists('spl_autoload_register')){

    spl_autoload_register(array('core', 'autoload'));

}else{

    function __autoload($class){

        return core::autoload($class);

    }

}

A:createapp();

class core
{

    private static $_app;

    public static function createapp(){

        if(!is_object(self::$_app)){

            self::$_app = 'something';

        }

        return self::$_app;
    }

    public static function autoload($class){

        $class = trim(strtolower($class));

        if(strpos($class, '\\') !== false){

            $path = ROOT;

            $path .= DS . $class . '.php';

            $path = preg_replace('/[\\|\/]/i', DS, $path);

            require_once($path);

        }

    }

}

class A extends core {}

and i got this error

Fatal error: Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static method (class 'core' not found)' in C:\xampp\htdocs\test\system\core.php:14 Stack trace: #0 C:\xampp\htdocs\test\system\core.php(14): spl_autoload_register(Array) #1 {main} thrown in C:\xampp\htdocs\test\system\core.php on line 14

after i read the error msg, it mean the class core not found? but the class is written in the same file???

geass
  • 63
  • 1
  • 4

1 Answers1

5

You must set a fully qualified name. Because your class is in system\core namespace:

spl_autoload_register(array('system\core\core', 'autoload'));
meze
  • 14,975
  • 4
  • 47
  • 52
  • Thanks, it worked, forget i'm using namespace, will set it as answer after 2 minutes – geass Oct 31 '13 at 09:02
  • I'm having this same problem. In my case, the class containing the autoload function is in another file from the `spl_autoload_register` call. It works when I copy the class into the same file as the `spl_autoload_register` call. – Jay Bienvenu Apr 05 '17 at 17:20