Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, How to set include path

Tags:

include

path

php

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?

like image 326
musicliftsme Avatar asked Jul 21 '11 17:07

musicliftsme


3 Answers

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).

like image 113
Brad Christie Avatar answered Nov 11 '22 10:11

Brad Christie


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.

like image 37
Paul DelRe Avatar answered Nov 11 '22 11:11

Paul DelRe


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.

like image 3
Marc B Avatar answered Nov 11 '22 11:11

Marc B