Results 1 to 6 of 6

Thread: What are the require_ once and include_ once in PHP?

  1. #1
    Join Date
    Jul 2006
    Posts
    273

    What are the require_ once and include_ once in PHP?

    Hello everyone,
    I have done some basic things in PHP programming language. I am newly started this language, so I don't have much knowledge about it. I want to know when to use require_ once statement and include_ once statement in PHP coding. I don't know anything about it, so it would be better if you explain me in detail. Please tell me what are the require_ once and include_ once in PHP? Help me soon.!!
    (\__/)
    (='.'=) This is Bunny. Copy and paste bunny into your
    (")_(") signatureto help him gain world domination

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: What are the require_ once and include_ once in PHP?

    The statement require_once () is identical to require () apart from that PHP checks if the file was already included and if so, do not include a second time. The include_once () statement includes and evaluates the specified file during script execution. The behavior is similar to include (), but the difference is that if the code has already been included, it will not be a second time. Hope that you know about the include() and require().
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Aug 2006
    Posts
    227

    Re: What are the require_ once and include_ once in PHP?

    To include/require a whole folder of classes can be stripped down to a one-liner :
    PHP Code:
    <?php 
    array_walk
    (glob('./lib/*.class.php'),
    create_function('$v,$i''return require_once($v);')); 
    ?>
    If you get "failed to open stream" although you are sure your include_path is correct, check your open_basedir setting.
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  4. #4
    Join Date
    Jul 2006
    Posts
    286

    Re: What are the require_ once and include_ once in PHP?

    When using absolute_path there are fewer stat() system calls. When using relative_path there are more stat() system calls because it has to start from the current directory back up to / and then to the include/ directory. So I would like to suggest you to try to use absolute_path when calling require(). The time difference between require_once() vs. require() is so tiny, it's almost always insignificant in terms of performance. The one exception is if you have a very large application that has hundreds of require() calls.
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: What are the require_ once and include_ once in PHP?

    The include_once () is used in cases where the file will be included and evaluated more than once in a script, or if you want to be sure that it is included only once to avoid redefinitions of functions or classes. The language of instruction include () statement includes and evaluates the specified file. The files are included following the file path provided, if none is provided, the include_path will be verified. The language of instruction include () will issue a warning if it does not find a file while the language of instruction require () will issue a fatal error in the same case.

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What are the require_ once and include_ once in PHP?

    I think that using the include_once() in the __autoload() function is redundant. When the PHP is not able to find your class definition, then autoload() must be called. If your file containg your class was already included, the class defenition would already be loaded and autoload() would not be called. So by using the include() within autoload(), you can decrease a little overhead. If you include a file that does not exist with include_once, the return result will be false. If you try to include that same file again with include_once the return value will be true.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,207,213.93848 seconds with 16 queries