Is it recommended to use loose Return in a PHP file? When?

Asked

Viewed 84 times

8

It is good practice to give a Return in a simple PHP file example:

<?php

$config['teste'] = 123;

return $config;

I’ll explain better I have this file di/dependencias.php:

<?php


$container['daoExemplo'] = function ($c)
{
    return new ExemploDAO();
};

return $container;

I need to add all the Anonimas functions contained in this file in a method, in case it will go through the whole di/ example directory:

public function requireAllDependencies($directory)
{
    try {
        //setando o diretorio e o tipo de arquivo a ser carregado
        $dir = "{$directory}/*.php";

        //Percorrendo o diretorio e pegado o caminho dos aquivo
        foreach (glob($dir) as $filename)
        {
            //Adicionando as dependencias
            $dependencies = require $filename;
            $this->add($dependencies);
        }
    } catch(Exception $e) {
        echo "Erro ao tentar adicionar o arquivo: " . $e->getMessage();
    }
}

In that example it would make sense ?

  • It makes sense that?

  • What is the objective?

  • 1

    @rray Incredibly, in certain cases does.

  • I’ve added a few more explanations, I hope it makes more sense.

1 answer

13


In certain cases this makes sense. Forget this question of good practice, do not use if you do not know exactly what you are doing.

The php manual explains well how the return when used out of functions, either in the main file or in includes (are different behaviors):

If called in global scope, the current script execution is completed.

If current script file is included or required with the functions include or require, control is passed back to the script you are calling. Also, if the current script has been included with the function include, the amount reported to return will be returned as the value of the include.

According to this information, your example of use is valid yes.

  • 7

    +1 for "Forget this good practice issue, don’t use it if you don’t know exactly what you’re doing." I think the most important part of the answer :P I think I’ll use this phrase in my answers :D :D :D The rest is right and good too :)

  • 1

    You can use at will, @Maniero :D

  • 2

    To quote, in PHP this is used to facilitate the maintenance of the code and, why not, increase the readability of it. It is much easier to understand, for example, what is a variable $config when that receives the Return of include than having that variable magically included in the scope. In addition it is possible to resolve name conflicts in the scope with this practice - when in the included file is returned directly the value and not a variable, for example.

  • 1

    I think so too, @Andersoncarloswoss. I was thinking of adding your comment to the answer, but since the question talks about good practices, I’m afraid I’ll encourage people who don’t pay attention return; in every file.

Browser other questions tagged

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