PHP, How to set include path

68,699

Solution 1

Common practice is to have a "common.php" or "includes.php" file that includes the include/include_once calls (for the sake of simplicity). e.g.

  • root
  • [lib]
    • a.php
    • b.php
    • includes.php
  • index.php

Then includes.php contains:

<?php
  include_once('a.php');
  include_once('b.php');
?>

Then in any script it's a matter of including the includes.php file.

However, to answer your original question, you can only include one file at a time, per call. You can use something like opendir and readdir to iterate over all files in a specific directory and include them as found (automated so-to-speak) or write out each include yourself based on the files you're creating.

Also, all setting the include path does is set a directory to look in when an include call is made. It's not a directory where the files should automatically be loaded (which is the impression I get from your post).

Solution 2

Setting the include_path will not include every file in that directory, it only adds that directory to the list PHP will search when including a file.

Specifies a list of directories where the require(), include(), fopen(), file(), readfile() and file_get_contents() functions look for files.

Source

This would simplify including files in a deep structure or in a completely different section of the filesystem.

include('/var/somewhere/else/foo.php');

With /var/somewhere/else/ added to the php.ini include_path could become

include('foo.php');

Additionally, as others pointed out, there are common practices but you could look into OOPHP and autoloading classes. This will not work for functions that I know of.

Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).

In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

Solution 3

I have a function like this in most of my projects:

function appendToIncludePath($path)
{
    ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . BASE_DIR . $path . DIRECTORY_SEPARATOR);
}

Solution 4

PHP's parser is pretty efficient - you'll waste a lot more time loading a ton of individual files instead of one (or a few) more monolithic files. However, if you insist on keeping things segregated like that, you CAN create meta-include files to load sets of individual files, so you'd only include the one single meta-include file, and it does the rest for you:

meta.php:

include('a.php');
include('p.php');
...
include('z.php');

And then you simply do:

<?php

include('meta.php');

in your scripts and you've got all the individual ones loaded for you.

Solution 5

see this question:

How to include() all PHP files from a directory?

Also, in terms of best practices, you can include multiple functions in the same file if they are at all related, and I would also suggest having more descriptive names of your functions and files. For example, if your a() and b() functions both related to validation for example, name your file validation.php and put both functions in there and try to rename them to something that is related to what they do. This will allow you to remember what they do when you start piling up a huge list of functions ;)

Share:
68,699
laketuna
Author by

laketuna

Updated on February 02, 2020

Comments

  • laketuna
    laketuna about 4 years

    I've been writing:

    include('a.php')
    include('b.php')
    

    etc. in my script to include to use functions a() and b(). It gets pretty tedious. Is there a way to set a path of a directory and have multiple files there be accessible by a script?

    1. I tried this in my script:

      set_include_path('.;C:\xampp\htdocs\myfunctionfolder');
      
    2. And I set an environmental variable PATH to have this older in there.

    3. I also in modified php.ini to include

      include_path = ".;\xampp\php\PEAR;\xampp\htdocs\myfunctionfolder"
      

    If I have many files in there, how do I access these files without having to include each individually? Setting the environmental variable definitely works in the command prompt.

    Do I need to do something else for .php files to be accessible collectively under a directory?

  • laketuna
    laketuna almost 13 years
    Hmph!! it's all clear to me now. Thanks for showing me the common way. I'm still trying to figure out what's "conventional" in PHP.
  • Brad Christie
    Brad Christie almost 13 years
    If this is your own personal project, "conventional" is whatever way works for you. The moment you have to start using the find function(s) or write file names down with notes you're losing sight of "conventional". ;-)
  • laketuna
    laketuna almost 13 years
    if I include "include.php" that loads up a bunch of .php files (say twenty ~50-line functions) that I may or may not need, would this impose some kind of unnecessary load when running the script that include these? I'm wondering if I should customize my include.php or just make 1 that includes all of my functions.
  • Brad Christie
    Brad Christie almost 13 years
    @user: Depends. Maybe make a "common.php" that has functions used on 90% of the pages, then a "user_includes.php" with user functions only used on member pages, etc. Go download phpBB and look how they set their structure up, that should give you some good insight on how to proceed.
  • andreszs
    andreszs about 7 years
    No one considered the fact that on certain cases, includes MUST be loaded on different sections... for example, if you include the header, sidebar and footer of a page layout, you just can't include them consecutively.
  • PokatilovArt
    PokatilovArt over 3 years
    Worked for me on Ubuntu, way with PATH_SEPARATOR haven't worked.