PHP,如何设置包含路径 [英] PHP, How to set include path

查看:134
本文介绍了PHP,如何设置包含路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在写:

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

等。在我的脚本中包括使用函数 a() b()。这变得非常繁琐。有没有办法设置目录的路径,并且脚本可以访问多个文件?

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. 我试过这个在我的脚本中:

  1. I tried this in my script:

set_include_path('.;C:\xampp\htdocs\myfunctionfolder');


  • 我设置了一个环境变量PATH来让它更老了。

  • And I set an environmental variable PATH to have this older in there.

    我还修改了php.ini以包含

    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.

    我是否需要为.php文件做一些其他事情才能在目录下集体访问?

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

    推荐答案

    通常的做法是有一个common.php或includes.php文件,其中包含 include / include_once 调用(为简单起见)。例如

    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

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

        然后 includes.php 包含:

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

        然后在任何脚本中都包含 includes.php file。

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

        ,为了回答您的原始问题,每次调用一次只能包含一个文件。您可以使用类似 opendir readdir 的内容来迭代特定目录中的所有文件并将其包含在找到的位置(自动化)可以根据你正在创建的文件写出每个包含你自己。

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

        这篇关于PHP,如何设置包含路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆