开发者

PHP, How to set include path

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 scr开发者_JS百科ipt:

    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?


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


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.


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.


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);
}


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


include __DIR__ . '/../folder1/folder2/folder3/Target.php';
include __DIR__ . '/../folder1/folder2/Target.php';

It helps you go to any path.


Download latest PHP zip and extract to C drive then download composer and install it, during installation it ask for PHP path so just select extracted PHP path.

As follow below step.

  1. Go to Computer.

  2. Select Advance System Settings.

  3. From system properties select Environmental Varaibles.
  4. In Environmental Varaibles add Path in User Variable for PCNAME
  5. In Environmental Varaibles add Path in System Variables.
  6. Hit OK.
  7. Restart PC.
  8. Win + R type cmd.
  9. type command php -v.
  10. Vola you good to go.

PHP http://php.net/downloads.php

Composer https://getcomposer.org/download/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜