How to avoid the repetition of requires?

Asked

Viewed 115 times

0

I have this code that makes the includes:

<?php
session_start();
require_once "lib/conexao.php";
require_once "lib/functions.utilities.php";
require_once "lib/class.User.php";
$usr = new User(conectar());
timeValidation();
?>

But right now, I need to do a client part and I need to keep putting the ../?

<?php
session_start();
require_once "../lib/conexao.php";
require_once "../lib/functions.utilities.php";
require_once "../lib/class.User.php";
$usr = new User(conectar());
timeValidation();
?>

Could I tell him to look straight at the include without putting ../?

  • Ever thought of an autoloader ?

  • When you have a time, search on Composer. Composer do all this work to include the files you need.

4 answers

5

If I’m wrong, I’m sorry, but I suppose autoload() will do what you intend to elaborate, a simple example would be this below:

<?php

//jeito moderno
spl_autoload_register(function ($class){
    if(file_exists($class. '.class.php')){
        require_once($class. '.class.php');
    }
});


$obj = new autoload2();
$obj->setCor('azul');

echo "A cor da bola é: ".$obj->getCor();

echo "<br>";

?>

autoload2.class.php:

<?php

class autoload2{

    private $cor;

    public function setCor($c){
        $this->cor = $c;
    }

    public function getCor(){
        return $this->cor;
    }
}

?>

This is an example I have here on my computer.

2

I don’t think it’s necessary spl_autoloader for something so commonplace, much less PSR, after all is not trying to load classes based on namespaces, in fact I believe that one of the includes is not even a class.

So I would say just create a file called global.php and in it add everything you need, this way you would need to use the require_once only once for each document, for example:

global.php

<?php
session_start();
require_once "lib/conexao.php";
require_once "lib/functions.utilities.php";
require_once "lib/class.User.php";
$usr = new User(conectar());
timeValidation();

Then in both parts I would add only this:

<?php
require_once 'global.php';

And:

<?php
require_once '../global.php';

If you’re going to use spl_autoloader I recommend standardizing by following the namespaces and the PSR-4, as I explained in:

You can use the composer also, but not necessary, the advantage of the composer is more about being able to use third-party libraries easily in your projects.

  • 2

    Interesting and very simple your solution to something so trivial. Taking advantage, is there a gain/loss (that is not insignificant) using this way instead of using everything manually? Taking away the need to repeat.

  • @Uzumakiartanis I don’t even know if I used the word trivial at the right time, or if it was appropriate, maybe writing "something simple" would sound better? :P

  • 1

    Maybe common/simple/everyday is your word. Although, I think it was well used trivial.

  • @Uzumakiartanis "ordinary" :)

1

It’s not the best way to do it, I think it’s bad practice to do it, but you can create a file called "includes.php". In this file you give the require_once() of all the files you need. Once you’ve done this, you go into the client part and only one require_once() do "includes.php".

0

You can use the Composer.

You can create a file composer.json so at the root of the project:

{
    "require": {},

    "autoload" : {
        "psr-4" : {
            ""  : "src"
        }
    }
}

Where "" would be the namespace. However as it is not used, I left blank. "src" would be the root folder of the classes.

Example:

src/
   MinhaClasse.php

The content of the class MinhaClasse.php, has the following content:

class MinhaClasse
{

    public static function metodo()
    {
        echo 'método chamado', PHP_EOL;
    }
}

Then run the following command:

 composer dump

or

php composer.phar dump

A briefcase vendor will be generated. Then just apondar a single include to the file vendor/autoload.php, that everything will work. See:

require __DIR__ . '/vendor/autoload.php';


MinhaClasse::metodo();

The process is not difficult, I did exactly this test with less than 10 minutes and configured this example

Note: You can use the command composer init to generate the Composer.json file faster and easier.

Browser other questions tagged

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