473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best Organization of PHP Include Files?

Hi,

In my PHP code, I typically break my functions into groups (usually based on
the data type they operate on), and then include the files, i.e.:

require_once("string_functions.inc");
require_once("large_integer.inc");
etc.

The included files never contain any code that gets executed directly (it is
all in functions, which are then called from the code in the page that is
doing the including).

I've always found PHP to be very fast, but in large projects a large number
of files with a lot of functions could be included. I have to assume that
the PHP interpreter requires some time to parse each include file.

Questions:

a)How inefficient is this? (I.e. how much parsing does the interpeter do of
functions that are included but not yet called?).

b)What is the best paradigm for managing include files?

Thanks, Dave.
--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Mar 22 '07 #1
2 4365
David T. Ashley schrieb:
I've always found PHP to be very fast, but in large projects a large number
of files with a lot of functions could be included. I have to assume that
the PHP interpreter requires some time to parse each include file.
Yes, it does. But this should not be your intention for reorganizing the
file structure. Only ease of management and increased maintainability
should be of any concern here.
Questions:

a)How inefficient is this? (I.e. how much parsing does the interpeter do of
functions that are included but not yet called?).
First of all, the slowest part of the including is actually looking up
the file (include_path) and opening it. The parsing is very quick
compared to that. The filesystem part inproves over time because modern
filesystem do a good job of caching the accesses. Only the first lookup
is slow.

If you run into any performance issues then the first thing to do is
install an opcode cache like APC or eAccelerator because this gives
performance at almost no cost beside some RAM. These tools make it
possible to completely skip the lookup phase AND the parsing because the
most used code is kept in RAM in compiled form. A tip: Minimize your use
of the _once variants of include and require if you plan on using APC
because this MAY cause degraded performance of the cache.

This about a quick performance boost. Now to structural topics...
b)What is the best paradigm for managing include files?
Some considerations: First of all, you have to clearly seperate file
that are only included once per request from those you might be
including multiple times. All files declaring functions and classes may
only be included once because subsequent includes would result in an
error. These are "library files" Files containg just some HTML snippets
or simple linear PHP code may be included multiple times, so they
somehow all belong to some type of "template file". For these two types
of files the usual managing concepts only apply to library files. And
here are some of those concepts:

- Only include what you use. This is a goal not easily achieved as you
already said so you always have to find a compromise. But this rule
makes it clear that it is a bad idead to include all of your libraries
at the beginning of each PHP script.

- Use a clear scheme for filenames. Structure is key when it comes to
building up a code library. Only if you easily know where each function
is then you can avoid blindly including too many files.

- For larger applications (more than 30 files or so): Follow the front
controller paradigma. Always call index.php for your application and
handle the navigation with other parameters. Different PHP entry points
are fine for small projects because you can easily define which files
every entry point needs but you lose control when the project becomes
bigger.

- Tip: Use static class functions instead of plain functions. Why? First
of all, it reduces the "pollution" of the global namespace. If you
decide to use third party libraries at a later date then the risk of a
name collision is bigger with ordinary functions. Use classes and prefix
the class names with a unique prefix for all your classes. You could
for example use "DTA" as a prefix. And classes allow for the next tip ;-)

- Use __autoload(). A strict directory structure and the "one class per
file" concept help to automatically include only used code. For example,
if you put the class DTA_Beer_Brewery in the directory
DTA/Beer/Brewery.php somewhere in your include_path and you create a
matching __autoload function, the you can just forget about including it
manually. You just call DTA_Beer_Brewery::getGuinness() somewhere in
your code and the autoloading manages the inclusion of the file.
Hope that helps.
OLLi

--
Just to be absolutely certain: you are the female of your species?
[John Crichton, FarScape 113]
Mar 23 '07 #2
David T. Ashley wrote:
>
The included files never contain any code that gets executed directly (it
is all in functions, which are then called from the code in the page that
is doing the including).
V. Sensible.
I've always found PHP to be very fast, but in large projects a large
number
of files with a lot of functions could be included. I have to assume that
the PHP interpreter requires some time to parse each include file.
Sort of. It's got to parse the code whether its in one big file or fifty
small ones. Actually, that's not true all the time either - if you use a
code cache it doesnt't have to parse each file every time - and greater
granularity improves caching effectiveness.

There is a small overhead from loading additional files but I think its
usually worth spending on hardware what you're saving on programmer time.
b)What is the best paradigm for managing include files?
It depends. If you're using php5 an OO programming then fitting your include
files around the autoloader is a very good idea. If you run mutliple
different environments for development, testing, staging and publishing
then a hierarchy of include directories in your include_path allows you to
override different files at different stages of the deployment.

You might as well ask what's the best of car to buy - its the same answer,
the one that's right for you.

HTH

C.
Thanks, Dave.
Mar 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Digital | last post by:
Hello, I am developing an application for Windows 2000 and Windows XP. I plan on installing the application under The 'Program Files' folder. My application generates some log files as well as...
1
by: Steven T. Hatton | last post by:
I think Danny was one cup of coffee shy of full consciousness when he wrote this, but the gist of it makes sens to me: "C++ Project Organization Guidelines Last updated May 26, 2005....
2
by: sstevens | last post by:
Does anyone have suggesstions for ASP.NET deployment best practices. This is in the context of a large organization where developers do not have admin access to web servers. The idea of buliding...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
4
by: Ron | last post by:
Hi all, Trying to work out a few problems in using php on my site. Partially this is a html question. I was reading a lot of the posts and it seems that some of the includes people are using are...
3
by: mshngo | last post by:
Hi, I have seen two main methods of organizing the header and source files in a C++ project. The first method separates the public header files from the remaining files (i.e., source files and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.