SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict moonchild's Avatar
    Join Date
    Nov 2003
    Location
    U$A
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Require() within a loop?

    Hey all,

    I am very specific about where i store my files (i.e. classes go in root/lib/classes/ and functions in root/lib/functions/). would this code below work to include all the files from these directories without having to do each one manually? * haven't tried it yet because the last time i wrote a loop like this it froze the computer lol *

    PHP Code:
    // Initialize Variables
         
    $sysdir_classes;
         
    $openDir;
         
    $req_sys_file = array();
         
    $closeDir;

    // Include class files
         
    $sysdir_classes 'lib/classes/';
         
    $openDir opendir($sysdir_classes);
         while(
    $req_sys_file readdir($openDir)) {
              if(
    $req_sys_file != '.' && $req_sys_file != '..') {
                   require(
    $req_sys_file);
              }
         }
         
    $closeDir closedir($openDir); 
    i use all the files i write in my pages. so this would work perfectly for me if it actually works. =P

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     while(($req_sys_file readdir($openDir) !== false) { 
    Are you sure you need a relative path and not an absolute one?

  3. #3
    SitePoint Enthusiast jpp's Avatar
    Join Date
    Nov 2003
    Location
    Arnhem, The Netherlands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=moonchild]Hey all,

    I am very specific about where i store my files (i.e. classes go in root/lib/classes/ and functions in root/lib/functions/). would this code below work to include all the files from these directories without having to do each one manually? * haven't tried it yet because the last time i wrote a loop like this it froze the computer lol *

    PHP Code:
              if($req_sys_file != '.' && $req_sys_file != '..') {
                   require(
    $req_sys_file);
              }
         }
         
    $closeDir closedir($openDir); 
    You should use include instead of require in this construction:

    http://www.php.net/manual/en/function.require.php

    Looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.
    I never knew that, thanks a lot jpp.

  5. #5
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't know that either. I thought require_once() or include_once() would do such thing.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    Earth
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Toly
    I didn't know that either. I thought require_once() or include_once() would do such thing.
    _once is different.

    require is simply not affected by program flow. It works more like this... before php executes the code, it goes and replaces require() with the actual code from the require'd file, and then pass it on to the php interpreter.

    The include() on the other hand, gets executed as the code executes the loop so it will include the same file repeatedly.

    The manual says this was the behavior prior to php 4.0.2 hmm I wonder how it works now. I'll do a test

    From the manual:
    require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless.

    _once is another thing alltogether
    http://www.quadrahosting.com/
    Multi Domain Web Hosting service for newbie and experienced developers


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
  •