Domanda

I have a folder structure that looks like

base_dir-
        Includes.php
        Libs-
             Database.php
             Log.php
             Cofing.php
        Models-
             someClass.php
        Scheduled-
             test.php

My Includes.php has

spl_autoload_register(NULL, FALSE);

spl_autoload_extensions('.php, .class.php, lib.php');

function libLoader($name) {
    $file = 'Libs/' . $name . '.php';
    if (!file_exists($file)) {
        // throw new Exception("Error Loading Library: $file does not exists!", 1);
        return FALSE;
    }
    require_once $file;
}

function modelLoader($name) {
    $file = 'Models/' . $name . '.php';
    if (!file_exists($file)) {
        // throw new Exception("Error Loading Library: $file does not exists!", 1);
        return FALSE;
    }
    require_once $file;
}

spl_autoload_register('libLoader');
spl_autoload_register('modelLoader');

My someClass.php has

require_once '../Includes.php';
class someClass extends Database
{
    public function __construct() { return 'hello world'; }
}

And test.php has

require_once '../Includes.php';

try {
     $loads = new someClass();
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage();
}

When I run test.php I get someClass not found on .../Scheduled/test.php

Does spl works with extended classes like someClass.php or do I need to include the class to be exended?

And why it wouldnt find someClass.php?

Thanks

È stato utile?

Soluzione

Change

$file = 'Models/' . $name . '.php'; 

to

$file = __DIR__ . '/Models/' . $name . '.php'; 

in your models autoloader (and the equivalent in your libLoader) to ensure that it's searching from the correct directory, and not the directory where your test.php file is located

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top