How to load autoload classes on web and local server

Asked

Viewed 411 times

1

I need to make my file that makes the class instance of my project work on both the web server and the local server.

This is the file that makes autoload, autoload.php

<?php
spl_autoload_register ( function ($classe){ 
    $classesDiretorio = __DIR__.'/classes/';

    $classesArquivo = $classesDiretorio . ' / ' . $classe . '.php';

    if(file_exists($classesArquivo)){
        require_once ($classesArquivo)
    }   
});

In xampp, it works well, but when I go to the site, it gives error in the address of the path.

Follow the error:

Fatal error: Class 'Client' not found in /home/diego325/public_html/temporario/rental/cadastro.php on line 13

How can I get autoload to take the paths from both the local server and the web server?

I believe my problem is similar to that:

Autoload problems in PHP

But in the problem mentioned above there was no solution.

  • There is a directory called classes in /temporario/rental/, containing the file Cliente.php?

  • 1

    Apparently there is nothing wrong. The code presented works well in diverse environments, except for the use of constant __DIR__ because this is available from PHP5.3 http://php.net/manual/en/language.constants.predefined.php. If this is not the problem, check that the filenames are in agreement, with upper and lower case letters. The linux file system is case sensitive. If the file is php client., will give this error. It must be Php client. (I presume).

  • Opa Wanderson, yes. This is the one I use to instantiate the Client class.

  • So Daniel, this is it DIR same that is giving the problem. In my case it is linux server same. The php version on the server is 5.6.30. I tried to make use of $_SERVER values, but I was unsuccessful.

  • 1

    you can always try replacing the __DIR__ for dirname(__FILE__) and see if it solves the problem. You should also take into account the /, which may vary at the server’s discretion, DIRECTORY_SEPARATOR ?

2 answers

0

Since the post is old, maybe you. have already solved... I use Xampp (Macos) and always put my includes out of the root, for safety. But I don’t use the DIR and yes $_SERVER['DOCUMENT_ROOT'] to set the path, either on the local server or remote:

    define('INC_PATH',str_replace('/www','',$_SERVER['DOCUMENT_ROOT']).’meudiretoriodeincludes/‘);

or, depending on the remote server, add a bar before the includes directory:

    define('INC_PATH',str_replace('/www','',$_SERVER['DOCUMENT_ROOT']).’/meudiretoriodeincludes/‘);

Solved the path...

0

DEFINE('DS', DIRECTORY_SEPARATOR);
DEFINE('ROOT', dirname(__FILE__));
DEFINE('PUBLIC_PATH', ROOT . DS . 'public');
DEFINE('PRIVATE_PATH', ROOT . DS . 'private');
DEFINE('LIB_PATH', PRIVATE_PATH . DS . 'lib');
DEFINE('CLASS_PATH', LIB_PATH . DS . 'classes');

spl_autoload_register(function($class){
    foreach(get_defined_constants(true)['user'] as $path)
    {
        if($path === DS){ continue; }
        if(file_exists($path . DS . $class . '.php')){
            require_once $path . DS . $class . '.php';
        }
    }
});

Other than that, I personally don’t know how efficient or problematic may be, yet it’s functional. You can also choose the setting via arrays, specifying the directories to be checked for automatic loading that I find much easier to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.