Instead of using manual includes, why do not you use an autoload for all required classes?
define('PS', PATH_SEPARATOR);
define('DS', DIRECTORY_SEPARATOR);
set_include_path(get_include_path() . PS . 'diretorio' . DS);
spl_autoload_extensions('.php, .inc');
spl_autoload_register();
You can do this to limit the files "suaclasse.class.php":
define('PS', PATH_SEPARATOR);
define('DS', DIRECTORY_SEPARATOR);
define('CLASS_DIR', 'class' . DS);
set_include_path(get_include_path() . PS . CLASS_DIR);
// se quiser atribuir um "class" aos arquivos '/class/file.class.php'
spl_autoload_extensions('.class.php');
spl_autoload_register();
In fact, as noted by @WallaceMaxters, the correct one is to use: PATH_SEPARATOR
There's another interesting way to do that is through composer.phar :
maintaining the file: composer.json
{
"name": "empresa/seuapp",
"description": "Nome do APP",
"require": {
"phpunit/phpunit": "^4",
"php": ">=5.3.3"
},
"license": "MIT",
"authors": [
{
"name": "seunome",
"email": "[email protected]"
}
],
"minimum-stability": "alpha",
"config": {
"vendor-dir": "vendor/"
},
"autoload": {
"psr-4": {
"SeuApp\": ["src/"]
}
},
"comments": [
"- Para habilitar o autoload, use o comando: php composer.phar dump-autoload -o",
"- Para instalar o composer: php composer.phar install",
"- Para atualizar o composer: php composer.phar self-update",
"- Servidor web embutido: php -S localhost:9000 -t /var/www/html/seuapp/public"
]
}
Create folders:
/SeuApp/public/
/SeuApp/src/
/SeuApp/src/Controller/
/SeuApp/test/
Inside the Controller folder, include the file ApplicationController.php
:
<?php
namespace SeuApp\Controller;
class ApplicationController
{
public function controller()
{
return 'Olá Mundo';
}
}
And in the root of /SeuApp/src
, the file, Application.php
com:
<?php
namespace SeuApp;
class Application extends Controller\ApplicationController
{
public function index()
{
echo self::controller();
}
}
That's right, you already have the basics of an application using Autoload from the composer.