I have a single functions file for my entire site and given a single page 90% of the file is not even called. So i want to load only the functions which are called in the page and i am new to php.
-
3Unless you have something in the magnitude of thousands of functions in the file, I wouldn't worry about it. – JJJ Jul 28 '11 at 08:41
-
Related: [Autoloader for functions (19 Jan 2011)](http://stackoverflow.com/questions/4737199/autoloader-for-functions) – hakre Aug 27 '12 at 10:25
5 Answers
Group functions together into several objects with common traits, by making them static functions, and put each object in a separate file. Then use PHP5 autoloading functions to load appropriate objects only when they are used.

- 23,820
- 10
- 76
- 86
-
Objects, static functions, autoloading functions... I think that it is a little to much for a beginner. – sica07 Jul 28 '11 at 08:47
-
@scia07: objects with static functions is quite an easy concept and actually comparable to standard functions in the global namespace. And autoloading is very friendly as well. Some code-example would probably make this more easy to understand however. – hakre Jul 28 '11 at 08:57
-
any links you can refer me to so that i can learn more about auto loading and how to acheive it? – Pradyumna Challa Jul 28 '11 at 12:58
You can split this file into many small files and include only that you need to use.
However if it is not a big file it will not decrease your performance at all

- 148,279
- 62
- 259
- 315
You could split up your functions file in multiple files, but mind that if you need more files, loading may even be slower, because you need more IO commands to load the different files.
Futhermore, you split files by functionality. If you feel all these functions belong together, keep them together in that file. It will not slow down your script very much.
If you like, you can put the functions in (static) classes and use an autoloader to load the file, but I'm not in favor of this solution. I think static classes are just an excuse to get functions (and vars) out of the global scope, and creating classes just for auto-loading is abusing the autoload functionality. Of course, if you create a more object oriented script, using classes makes sense too, and auto-loading them might be convenient.

- 114,394
- 18
- 182
- 210
Actually it's not a problem to have a function available for use but you only use it in part of your application. It's more important you have everything when you need it at hand w/o actually caring about when to load.
If your system grows, what you might look for is an autoloader. PHP supports autoloading of classes but not for functions. However you can group your functions into classes (some will slap me for making such a statement) to make use of autoloading then.

- 193,403
- 52
- 435
- 836
If Your file size is not big then it will not decrease your performance at all.still if you want to achieve your goal then group related functions and place them in separate files and include only necessary files...

- 2,619
- 9
- 29
- 41