Multiple Function in one file

Asked

Viewed 85 times

3

I have a project in php and within that project I have a folder called Funções. Inside this folder I have several function files.

I have a file called Logado.php, where I authenticate the user and call the pages to be loaded inside it.

In every file I have on the site, I have to call the function file. I do it this way:

include "Funcoes/Inverte_Data.php";

What I wanted to know is, if I put all my functions inside a single file and call it in Logado.php, would slow down the system?

Because I think it would be much more practical, because since all the functions are in the main file, I’ll just be able to run, like:

inverteData($data);

What they recommend me to do?

  • 2

    I believe it’s slower to call multiple separate files than just a single one. If it is customary to call more than one file, I believe it is better to unite all functions in one.

  • 4

    How about you measure? If you can’t tell the difference it probably doesn’t matter.

  • 2

    I already thought about it some time ago as follows: "If so, wouldn’t it be better to comment elsewhere, since comment lines will also increase the physical size of the file and increase the weight of the file processing in the application?". Soon I think it’s better to have a well-organized archive inside.

  • I understand, but whenever I recharge the system, Logado.php it will load all functions, even those that will not be used. This will not slow down the system?

  • 2

    Loaded for you means she’ll be executed, even if she’s not called ? Why "loaded" I understand how it was imported into the code to be used when requested. Before that, she sits there very quietly and does not bother anyone.

1 answer

3

It is interesting to have several functions in a file as long as they deal with the same "subject".

For example, you have functions to handle date (reverse, brParaSql, Sqlparabr, etc.), then you can create an option with the class Dateutil and put these methods there, but this class cannot contain methods that are unrelated to date (e.g.. inverter) because it runs away from the responsibility of the class. These methods can be statistical, as they are methods that serve to assist and it is unnecessary to have to instantiate an object.

As for having to include several files, I see no problem, because if it was one, no one would use framewoks because they have many files. And to solve the problem of having to write a lot of require, take a look at PSR-0 which deals with autoload.

Browser other questions tagged

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