Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency for including files of functions (in PHP)

If I had a large number of functions would it be better to keep them all in one large file or would it be better to separate them into several files of related functions. By better I mean more efficient for both maintainability and for the server processing the request.

For example right now I have all my files in a file named include.php. But would it be wiser to have an include file of includes like:

<?php
 include('/functions/user.php');
 include('/functions/admin.php');
 include('/functions/content.php');
 include('/functions/nav.php');
 include('/functions/database.php');
 include('/functions/other_junk.php');
?>
like image 692
Josh Curren Avatar asked Jan 21 '10 03:01

Josh Curren


People also ask

How many built in function in PHP?

PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.

What is PHP include?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is function name in PHP?

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ .


2 Answers

Definitely separate them, for maintainability sake. I doubt performance will suffer at all, but even if it does (just a teensy bit) you're better off writing maintainable, readable code.

like image 95
echo Avatar answered Nov 15 '22 16:11

echo


You want to be sure you're using a PHP cache like XCache or APC. Your PHP files should then all be in memory and you shouldn't be worried about your includes hitting the disk at all.

I would definitely find it easier if you broke up like minded functions/classes into their own files.

like image 31
Klinky Avatar answered Nov 15 '22 14:11

Klinky