A curiousity regarding include vs. include_once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

A curiousity regarding include vs. include_once

Post by ReverendDexter »

It seems to me that for "good, clean code", you would *always* want to use a xxxxx_once() function.

Is there any reason why you would prefer to use include (or require) instead of include_once (or require_once)? Is there less overhead, or would there be a reason to inlcude/require something multiple times?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

In procedural programming, using an "include" essentially inlines the code you want into the PHP document. Using functions, however, should make this unnecessary, and this sort of system can get very messy if not done with discipline.

There is less overhead for just include/require versus include_once/require_once, however, it is usually quite negligible.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I always use include_once, because I set up every file to be able to work independently, so they need to each be able to include the necessary files. However, since a lot of those overlap, include_once helps out.

The alternative to it would be how C++ header files are handled by giving each one a unique definition and checking if it is defined(). As long as include_once is available, I'll chose that method over the C way.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I <3 namespaces...

Code: Select all

from mylib.forms import myform
import mylib.settings

def someFunc():
    print myform(settings.form)
Post Reply