tags:

views:

33

answers:

4

Hi all,

I use the following procedure to call the functions within the pages of my web app.

//index.php
include("functions.php");
include("file1.php");
include("file2.php");

I have all my functions going into functions.php page. The content of this page may be over 5000+ lines of code and it contains all the functions used within the website. So loading 5000+ lines of code in all the pages of my website, even when it's not needed, seems like a lot of load. So my questions is:

How to load only functions that are needed and only on demand without having to create a separate functions page for each of my pages?

Please consider this example:

//functions.php contains functions f1 through 10
function f1() 
{
  //do something
}
function f1() 
{
  //do something
}
...(through)
function f10() 
{
  //do something
}

If index.php page makes use of only functions f1 and f2, how can I load only those 2 functions on that page, without having to load all the rest of the functions (f3 through f10)? Please note that my app. is using mysql database(if that helps).

Also maybe it's worth mentioning again that one idea that I already have is that I will need to create functions for each of my pages i.e, functions_index.php page for use in index.php page and likewise, create different function pages for the rest of the pages in my app. While at once this seems like a good idea, I may end up duplicating the same function(s) over and over and this can lead to heavy duplication. My sole aim is to keep the functions centrally accessible by all the pages, yet load the functions only on demand. Hope this is possible.

Thank you.

Note: Please note that all the code written is mainly done via functions and IS NOT OOPS based. So I would really appreciate any solutions that can be implemented without having to switch to using OOPS concepts. Thanks again.

A: 

first of all, load 5k lines of code is not that hard for php, but other than that, you should seriously think about splitting your single file into separate, easy to handle chunks and while you are at it you could even start coding in an oop way

roman
That being said, the more files included in the more overhead you'll incur.
Xorlev
+1  A: 

If you switch to an OO design, you can create an autoload function (and register it with spl_autoload_register) to load classes on-demand.

Other than that, perform some profiling to see what the impact is of loading all your functions. Performance-wise, there might not be a significant impact relative to other aspects. There's a design-wise impact of having a monolithic file containing all the functions you may need, namely the danger of increased coupling. You decrease coupling by separating concerns.

outis
A: 

There's no really clean way to get PHP to load certain functions in certain places without a tradeoff involving code duplication. For example, you could separate your functions into logical classes, and then load only the appropriate class for the appropriate section - but those classes can't overlap, or you'll end up duplicating code and defeat what you're trying to do in the first place (and there's certain stuff that will always overlap).

However, if you're concerned about loading the same 5000+ line file repeatedly all over the place, the real questions are:

1) Are you seeing actual degraded performance because of this? If not, why worry?

2) If you are, have you thought about using APC (Alternative PHP Cache)? That would cut out repeated parsing of that file, though it would still get executed each time.

beamrider9
A: 

Can you divide them into groups that make sense?

For instance:

  • Database Functions
  • File Management functions = String functions

If so, it may work for you to do something like that and then include only the groups you want.

page 1:

include_once("functions/db.php");

include_once("functions/strings.php");

and page 2 could be:

include_once("functions/files.php");

include_once("functions/strings.php");

easement