Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it best to require a large file or more calls to fewer files? 1

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
Hi all,
I'm trying to optimise my website. I have a large "functions" file. It is 'included' on every page. Is there any speed gain to having one large functions.php file OR is it faster to have an include file that checks what page the visitor is on and then 'includes' a specific small functions file based on that page.

i.e. which is quicker...

1 File:
function myFuncOne() {}
function myFuncTwo() {}
...
function myFuncHundred() {}

OR...
Several files:
if(page == "registration") {
include("registration_functions.php");
}
if(page == "profile"){
include("profile_functions.php");
}

Does anyone know whether there is any significant difference between these two methods in terms of web page speed etc

Cheers

JT ;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
Just try it. How fast a file is opened and how fast one is read is quite dependent on your OS and system settings, I think.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
i agree with DQ on the dependency on your specific set up.

However it looks (from your code snip) that the question you should have asked was 'is it better to have all my despatch functions in one file or just 'require' the relevant subset each page refresh?.

in this case, i would then to go for the include route assuming that your script is at least modestly complex.

my current practice is as follows:

index.php calls
includes.php calls
functions.php
despatch.php calls
relevant subset

AND the functions.php file has an __autoload function which automagically includes the relevant class definitions.
 
OK, Thanks for your input.

I'm leaning towards your way jpadie as it is cleaner and easier to maintain.

Thanks for the advice.

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
remember that maintainability and cleanliness is a personal matter UNLESS you are developing an app that might be maintained by other people.

So for your own stuff - do what comes naturally.
and for group stuff the critical thing is to document what you are doing and how you are doing it. keep a functional map for all the key user tasks and use proper php docblocks for every function, method, class and variable. with the combination of these two sets of documentation your code should be easily maintainable even if you were to bung everything into a single file.

the other advantage of the include/despatch method is that your code will start running faster as php has to parse less (php will only parse a file once it is included). this may be of advantage if your site is heavily trafficked (although it then might be better to use pre-compilation techniques, cacheing and also handing off some php overhead to some native c code.

 
Thanks for that jpadie. This is just a personal site but I do like to keep things as 'tidy' as possible for my own peace of mind.

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
Remember that each time you include a file it does take time, so don't get to a popint where you have 10's of files to open just to do anything. I belive the Zend framework suffers because of this but I can't comment on that as I don't use it.
 
I've also had this question in the back of my mind for quite some time now. I have a basic function file that contains all the functions most commonly used on each page. I then had a ?category?_functions.php that contain specific functions for that category. My mothership is control.php where all files/fucntions/processes spawn from. It works quite well and is easy to follow.

ex:
control.php
------------
Code:
switch ($_GET['l'])
{
   case "home" : include "home.php"; : break;
   case "registration" : include "registration.php"; : break;
   case "contact" : include "contact.php"; : break;
   default : break
}

each include contains a call to a specific function subset.

-Geates
 
Geates
- there is an even easier way to do what you want, without all the various switch cases

Code:
if (isset($_GET['1'])){
  if (file_exists ($_GET['1'].'.php')){
    require_once ($_GET['1'].'.php');
  }
}

based on your submitted code, the above should be able to replace your entire despatch file, more or less.
 
I like the idea of that...

Where's the:
$_GET['1'] coming from?

Are you deriving it from $_SERVER['REQUEST_URI'] or something like that???

Thanks

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
GET variables come from the query string. they are superglobals.
 
Hi

Security maniacs may say that an [tt]in_array()[/tt] should be added to avoid the inclusion of PHP files not intended for inclusion in that place and/or moment :
Code:
[b]if[/b] [teal]([/teal][b]isset[/b][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'l'[/i][/green][teal]])[/teal] [teal]&&[/teal] [COLOR=darkgoldenrod]in_array[/color][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'l'[/i][/green][teal]],[/teal][b]array[/b][teal]([/teal][green][i]'home'[/i][/green][teal],[/teal][green][i]'registration'[/i][/green][teal],[/teal][green][i]'contact'[/i][/green][teal])))[/teal][teal]{[/teal]
  [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]file_exists[/color] [teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'l'[/i][/green][teal]].[/teal][green][i]'.php'[/i][/green][teal]))[/teal][teal]{[/teal]
    [red]require_once[/red] [teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'l'[/i][/green][teal]].[/teal][green][i]'.php'[/i][/green][teal]);[/teal]
  [teal]}[/teal]
[teal]}[/teal]

Feherke.
 
QUOTE
GET variables come from the query string. they are superglobals.
/QUOTE

Hi jpadie,
I understand that there are GET and POST vars but I wondered what $_GET['l'] is 'getting'...

Is it getting the first element of the GET var or is it a literal in this sense. ie. Are we passing a page like:

Thanks for any clarification.

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
that's right. not necessarily the first though. could be
Code:
[URL unfurl="true"]http://www.domain.com/somepage.php?somefield=somevalue&1=someothervalue[/URL]
 
Thanks for clearing that up for me.

Much appreciated

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
i would not advise using a 1 though. i'd be inclined always to use an alphabetic character as a variable name and not a number.
 
I wouldn't use GET anyway. I think it is too insecure. People can pass anything into your script that way. I think I'd be inclined to do some manipulation of the SERVER vars and find the page requested that way..

Still... some good info in this thread.

Thanks

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
i can't see how you can spoof a GET variable and not spoof the REQUEST_URI.

but i agree with feherke above that using in_array would be a good first stage protection. i was assuming that the file_exist() would be adequately limiting as you could control what was contained in the relevant directory. but feherke's solution is more elegant and allows for alternative file-structures.
 
jpadie

I agree with your comments about feherkes post being good first stage protection.

I was thinking along the lines of using the page address eg. if someone visits:

Then pictures.php would run the script to "include" myFunctions/pictures.php

ie. I would make the functions that pertain to each page be in a php file of the same name... (if that makes sense).

;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top