473,796 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("s tring_functions .inc");
require_once("l arge_integer.in c");
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 4392
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_Brewer y 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_Brewer y::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
1167
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 several types of data files. I know that I cannot create/write to files under the 'Program Files' folder unless I am a power user or an administrator. I would like my application to be run by a normal user. What are the best practices for choosing...
1
10924
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. http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=175 Last week's article about inline functions subtly brought into the limelight another important issue, namely how to organize the files of a typical C++ program, or project. This week I...
2
3421
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 msi packages for each deploy seems difficult to implement because the server admins do not have the time to constantly be running msi's and moving applications between environments. Are there any proven products that can automate this process for...
14
6707
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" process I am introducing namespaces to properly compartmentalise sections of the code into logical units. What I am speciffically trying to get right is the dependency tree for header files to reduce compile time and simplify the code structure. On...
16
2820
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 efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
13
2508
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 experience. We employ some parts of XP such as pair programming, and this should help. Other than books, does anyone have any suggestions? His skill set is pretty antiquated and we need to get him up to speed as quickly as possible so any suggestions...
49
3949
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 isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
4
1835
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 very complicated because of sessions or id's and variables I don't really know. So... Right now i use php for includes and getting some lists I make using mySQL and phpmyadmin and can get those out without error. For the most part a lot of my...
3
3610
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 internal/ implementation header files) -- the public header files are usually stored in a directory named "include", and the remaining files are stored in another directory named "src". The structures of these two directories are usually closely...
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.