Re: Including Functions; one file or many?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mark Kelly wrote:

Hi

I'm writing a set of db abstraction functions for an internal app which will give us a set of simple function calls for dealing with the db, like
$result = db_AddEmployee($EmployeeData);
$EmployeeData = db_GetEmployee($EmployeeID);

etc.

There will be quite a few functions needed to deal with all the different ways the app touches the db, so my question is:

Am I better off putting all these functions into one big include file (which could get pretty big) or using a seperate 'include' file for each function?

I'm thinking about the tradeoff between simplifying code by only having a single include file (parsing a lot of functions that aren't used, but less disk access) and having several include files (no extra funcs but lots more disk access). I realise there probably isn't a 'correct' way to do this, I'm curious about which methods folk here use in situations like this.

TIA in advance for any advice,

Mark

Mark, first of all, have you considered writing a class/object to handle this? eg.
class EmployeeManager {
   function Add(data) {
      // add employee to database
   }
   function Get($employeeId) {
      return employeeData
   }
}
Usually objects/classes are used when it's logical to group a set of functions (members) together because of shared functionality/shared purpose. Not that you couldn't do it like you do, which isn't at all wrong either :)

Now, to your next question. I would say, the world is grey and your thoughts are either black or white. You're looking at extremes; store *everything* in 1 file or store *every function* in its *own* file. Well, I'll tell you, don't attempt the last one because it will just cause you a lot of pain having to manage so many files. As for speed, the last one (with 1 file per function) will be slightly slower, since the overhead of 1 (or was it 2?) statcalls per included file will slow it down. Not that it'll be even remotely noticable until you include thousands of files, but it's there (and you asked for it). Now, as to the other extreme, everything in 1. The overhead for this is minimal when it comes to loading, but the question I would ask myself is, "do I really need all those functions *everywhere* I include the file?". Usually, this is a no, you only need a couple. So basically what's hapenning is, you need a few, say 5 functions from the file, but you include it entirely, all 500 of em, just to get those 5. The overhead you would incur at this point isn't due to the stat calls but due to defining functions and never using them.

Right, now back to my advice. I advise you group together your functions in a logical order and then move them per group to separate files. Example:
employee_management_functions.inc:
   AddEmployee()
   RemoveEmployee()
   GetEmployee()
   etc.

employee_project_functions.inc:
   GetProjectByEmployee()
   SetProjectByEmployee()
   GetAllProjects()
   etc.

So, when you need just a few functions you can specifically include that part and use them.

goodluck,
- tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux