Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2012, 02:47 AM   PM User | #1
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 610
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Any harm in Duplicate Include/Require?

Is there any real harm if I end up having this code twice in a script...
PHP Code:
    // Access Functions.
    
require_once(WEB_ROOT 'utilities/functions.php'); 

It may sound like a funny question, but since I am using procedural code - and maybe didn't plan things out as well as I could have - it is conceivable that I could have the above code in "script_1.php" which requires/includes "script_3.php"?!

Maybe when I start on v3.0 and hopefully start using OOP, I can solve some of these issues, but in the interim, I am wondering if such a scenario is a major deal?

Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 11-22-2012, 04:57 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yes and no.
The _once is used to prevent conflict, so it won't attempt to reimport to the symbols table if it already has been (which would throw fatal for anything being redeclared such as functions). But it comes at overhead, so its a tradeoff. I find in OO development, its just far easier to use _once at let it do its thing than to figure out exactly where everything is chaining to my use commands. I haven't a clue what its actually doing under the hood on the use calls though; I've always assumed its the same as issuing a require_once.
Fou-Lu is offline   Reply With Quote
Old 11-22-2012, 06:54 AM   PM User | #3
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
I think this is exactly why _once exists. As Fou-Lu suggested it's a good idea to keep a sense of your file interrelations, and kind-of not go nuts on the duplicate requests. However, if you've got yourself into a corner — or just don't know for that matter — then using _once should definately keep you out of any real trouble with multi-inclusion.

One important pitfall to note, consider this:

PHP Code:

 
require_once "file.php";
 require 
"file.php"
This will execute file.php twice, since a non _once inclusion will not even check if a _once has occured previously. If you want to be safe, use _once with all inclusions!
Custard7A is offline   Reply With Quote
Old 11-22-2012, 05:28 PM   PM User | #4
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 610
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by Fou-Lu View Post
Yes and no.
The _once is used to prevent conflict, so it won't attempt to reimport to the symbols table if it already has been (which would throw fatal for anything being redeclared such as functions). But it comes at overhead, so its a tradeoff. I find in OO development, its just far easier to use _once at let it do its thing than to figure out exactly where everything is chaining to my use commands. I haven't a clue what its actually doing under the hood on the use calls though; I've always assumed its the same as issuing a require_once.
I am thinking that what I need to create is some "universal include" - almost like a "bus" - that I include at the top of most of my scripts.

Of course, that is where the problem lies...

If I put something like my Database Connection into some sub-script that I require in every base script, then if I don't need the Database Connection, I have unnecessary overhead.

And if I just put the Database Connection in each sub-script that needs it, then first of all, I may be creating more of a maintenance issue since it is many places, and secondly, if I nest the Database Connection in sub-scripts that need it, I might forget to add it to a parent script which needs it and does NOT require one of the sub-scripts that did have it?!

Following me?!

And that seems to be the idiosyncrasy that I am discovering in my code during my final Unit Testing...

I have scenarios where "script_1" needs "script_2", and "script_2" needs "script_3", but "script_3" refers back to "script_1". (Kinda loopy!!)

I'm probably not making sense, and just not sure how far "down the rabbit hole" I should go with my v2.0 code, since there are likely ways to eliminate this issue with OOP - or better design Procedural Code.

Sincerely,


Debbie

P.S. If anyone is overly interested, I can certainly post some specific code examples...
doubledee is offline   Reply With Quote
Old 11-22-2012, 05:44 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Unless you check something before attempting to include it, there will always be overhead.
PHP Code:
if (!defined('THISSCRIPT_H'))
{
    
define('THISSCRIPT_H'true);
    
// the rest of my code.
}

// included with:
if (!defined('THISSCRIPT_H'))
{
    require 
'thatscript.php';

It won't get any better, especially since php includes off of the executing script, not the script including it. Hence why simply using _once and including it everywhere its needed is better.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:16 PM.


Advertisement
Log in to turn off these ads.