SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is a better way to write file includes?

    Is there a way to a file just for initialization that will be included in other files?
    Say you have this file called initialization.php with all the classes and necessary functions defined (coming from different directories).

    But u want to include this initialization.php in other directories (meaning the included statement in the file will break).

    I hope its clear?

    Is there a better way of doing this? Such that the includes will not break? And, not using absolute path as that means work when shifting server.

  2. #2
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I know some people dislike magic, but I find __autoload to be incredibly useful.

    It has several advantages:

    -No "setup.php" type file for includes
    -Forces you to use a consistent naming convention
    -Only classes which are used by the current page are loaded into memory

  3. #3
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd seen the example in php.net. not exactly sure how it works. and, does it only work for classes?

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But u want to include this initialization.php in other directories (meaning the included statement in the file will break).
    I am not sure what you mean by this, but I have a PHP file that has information used on every page on the site. No matter where the page actually resides, it will load this default.php file. The code I place on every page will look something like this:
    <?php include($_SERVER['DOCUMENT_ROOT']."/includes/default.php") ?>

    I put all my PHP includes in the includes folder that is found at the public root of the site. No matter where I put a page on the site, that code will load.
    Each day is a learning experience.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I felt its hard to explain by words too. Did a sample image.



    The includes in ajax folder will break since its referring to an invalid directory. But I wish to use the ini.file.php in that folder.

    I tried playing with include_path thingy. But...can someone show me how to input multiple paths for Windows? Heres what I have at the moment:

    Code PHP:
    ini_set("include_path", ".;
    									D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gts\trunk\admin\ajax;
    									D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gts\trunk\admin\Control;
    									D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gts\trunk\admin\Model;
    									D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gts\trunk\admin\View;
    									D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\gts\trunk\admin\lib");

    Doesn't seem to be working.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you are trying to do, and maybe I am looking at it wrong, but what I would try would be to create a file named ini.file.php with this in it:
    <?php include($_SERVER['DOCUMENT_ROOT']."/control/a.php") ?>
    <?php include($_SERVER['DOCUMENT_ROOT']."/model/b.php") ?>

    Then I would put the ini.file.php either in the root, or create a folder named includes. Then call it with a:
    <?php include($_SERVER['DOCUMENT_ROOT']."/includes/ini.file.php") ?>

    All I do is dabble in PHP, so the experts may have a better solution.
    Each day is a learning experience.

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just realized that you seem to be running this on your local computer, and I have not done anything like that.
    Each day is a learning experience.

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by resting View Post
    I tried playing with include_path thingy. But...can someone show me how to input multiple paths for Windows? Heres what I have at the moment:
    (...)
    You're on the right path; Setting the include path to point to the location of your external files means that you can include it from anywhere in your application. You need to replace the backslashes with forward slashes (Since backslash is an escape character), and you need to use the constant PATH_SEPARATOR:

    PHP Code:
    ini_set(
      
    "include_path",
      
    ini_get("include_path")
      . 
    PATH_SEPARATOR "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/gts/trunk/admin/ajax"
      
    PATH_SEPARATOR "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/gts/trunk/admin/Control"); 

  9. #9
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    You're on the right path; Setting the include path to point to the location of your external files means that you can include it from anywhere in your application. You need to replace the backslashes with forward slashes (Since backslash is an escape character), and you need to use the constant PATH_SEPARATOR:
    That, and you should keep in mind that pointing to absolute directories could present an issue in portability, if you decide to switch servers someday. Here's pretty much how I would do it:

    PHP Code:
    $docroot $_SERVER['DOCUMENT_ROOT'];

    ini_set
      
    "include_path",
      
    ini_get("include_path")
      . 
    PATH_SEPARATOR $docroot '/trunk/admin/ajax' 
      
    PATH_SEPARATOR $docroot '/trunk/admin/Control'
    ); 
    That way, the document root is allowed to change without breaking your application. The added benefit is that this works on any server, and it works on Windows, *nix, and Mac alike.
    Yes, I blog, too.

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to consider using a configuration file with a define for the path. Our rule of thumb is to avoid relative paths whenever possible. Thus, you would have something like this in your configuration file:

    PHP Code:
    define('SYSTEM_CLASS_DIR''C:/httpd/Apache2/htdocs/classes'); 
    When ever you reference the needed class, you would use the corresponding require statement:

    PHP Code:
    require_once(SYSTEM_CLASS_DIR '/specificclassfile.php'); 
    This method will also save a lot of headaches if you move from one environment to another as all you need to do is change your DEFINE value.

    -Bing
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  11. #11
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see many great suggestions. thanks all.


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
  •