Is it possible to include more than one .php file in the route of a Require?

2

I did not know the best way to ask this question, but I came across the following doubt ...

Inside a document, I have some require_once (which I've been putting together for a while), in the code they look like this:

<?php
   require_once("diretorio/arquivo.php");
   require_once("diretorio/arquivo2.php");
   require_once("diretorio/arquivo3.php");
?>

Is it possible that instead of replicating a require_once, I do the way I'm going to drop it?

<?php
   require_once("diretorio/arquivo.php", "diretorio/arquivo2.php", "diretorio/arquivo3.php");
?>

Whether or not it is possible, what would be the best way to structure all of these require? One per line or all in one?

    
asked by anonymous 16.11.2015 / 17:16

3 answers

5

Let's say that all your includes files are in a folder called the includes part and its index at the root of the project, you can create a function that does all of these requires / includes.

Use glob() to list all php includes files and make a foreach.

Project structure:

Root 
   includes
   public
      html
      css
      js
index

This function should be in your index or config.

<?php

function includes(){
    foreach(glob('includes/*.php') as $arquivo){
        require_once $arquivo;
    }
}

includes();

Take a quick test create 2 or 3 files in the includes folder with the following content:

<?php
echo __FILE__ .'<br>';

Then call your index and see the result, something like:

projeto\includes.php
projeto\includes.php
projeto\includes.php

You can also create a file with constants from the main directories and pass one of them in place of that folder that is set to glob() .

    
16.11.2015 / 18:32
4

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.

    
16.11.2015 / 19:58
3

To help with the response of @rray , reinforce the idea of using a glob function joined with include/require

function glob_include($glob, $flag = 0) {
    foreach (glob($glob, $flag) as $file) {
         require_once $file;
    }
}

The use would be:

glob_include('diretorio/*.php');

Still another option is to use GlobIterator .

$it = new GlobIterator('diretorio/*.php');

foreach ($it as $file) {
    require $file->getRealpath();
}

In this case, I prefer to use getRealpath , because the path to be passed is absolute. I've heard it has its advantages;)

    
16.11.2015 / 19:50