How to avoid repetition?

0

I have this code that includes the following:

<?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 now, I need to do a part of the client and I need to stay putting ../ ?

<?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();
?>

Would you like me to tell him to look directly at the include without putting ../ ?

    
asked by anonymous 31.08.2017 / 18:37

4 answers

5

If I'm wrong I'm sorry, but I suppose a autoload() will do what do 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.

    
31.08.2017 / 20:15
2

I do not think it's necessary spl_autoloader for something so common, much less PSR, after all you're not trying to load classes based on namespaces , in fact I think one of the includes is not even a class.

So I would say that just create a file called global.php and add everything you need, so you would need to use 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();

So on both sides I'd just add this:

<?php
require_once 'global.php';

E:

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

If you use spl_autoloader recommend standardize following the namespaces and PSR-4, as I explained in:

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

    
01.09.2017 / 19:37
1

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

    
31.08.2017 / 19:55
0

You can use Composer .

You can create a composer.json file like this at the project root:

{
    "require": {},

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

Where "" would be the namespace. But since it's not used, I left it blank. "src" would be the root folder of the classes.

Example:

src/
   MinhaClasse.php

The content of 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 vendor folder will be generated. Then just put a single include for the file vendor/autoload.php , which will all 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 set up this example

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

    
01.09.2017 / 17:47