Instead of using manual includes, because you don’t 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 @Wallacemaxters observed, the correct is to use: PATH_SEPARATOR
There is another interesting way to do that is through the use of Composer.phar:
keeping 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 the 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 at the root of /SeuApp/src
, the file, Application.php
with:
<?php
namespace SeuApp;
class Application extends Controller\ApplicationController
{
public function index()
{
echo self::controller();
}
}
Ready, you already have the basics of an application using Autoload from Composer.
At my suggestion, make an inc.php file where you add all require’s, stating the complete relative address. Dai, whenever you need to make a require for some files, just set it to the
inc.php
.– user28595
Because it doesn’t matter of a file that contains all the files?
– Naldson
Good idea, this doubt arose me because everything presents itself inside a document (at the top) and sometimes it bothers me to see some 3 busy lines with 20 characters. I’m trying to adapt to the best code rules right now.
– Lucas Henrique
Yeah, since you’re on top of someone else you just import it ;)
– Naldson
How to set the path to access files on procedural system or How to echo a variable of this function?
– rray
Anyway, I can’t put multiple routes inside a require, right?
– Lucas Henrique
What do you call a route? it is possible to exchange that string for a variable or a constant yes!
– rray
What I mean is if there is any way to avoid replicating "require_once" and define only one with all paths. Or what would be the right way to do it, anyway, it was just a random question.
– Lucas Henrique
I get it, if the main files are in the same folder, you can list all these files and use one
foreach
to mount therequire
.– rray