Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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!

  • #4
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    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...

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •