How to load autoload classes in web and local server

1

I need to make my file that makes the instance of my project classes 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 fine, but when I upload to the site, it gives an error in the path address.

The following is the error:

  

Fatal error: Class 'Client' not found in   /home/diego325/public_html/temporary/rental/cadastro.php   online 13

How can I make autoload take paths both on the local server and on the web server?

I think my problem looks like this:

Problems with autoload in PHP

But in the problem mentioned above there was no solution.

    
asked by anonymous 10.06.2017 / 15:37

2 answers

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 do not know how efficient or problematic it can be, it's still functional. You can also choose the definition through arrays, specifying the directories to be checked for automatic loading, which I find much easier to use.

    
10.06.2017 / 17:40
0

As the post is old, maybe you. already solved ... I use Xampp (MacOs) and always put my includes out of the root, for security. But I do not use DIR but $ _SERVER ['DOCUMENT_ROOT'] to set the path, either on the local or remote server:

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

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

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

Resolved path ...

    
06.04.2018 / 20:25