define my own BASE_PATH vs. set_include_path?

后端 未结 2 1919
春和景丽
春和景丽 2021-02-20 16:21

I learned of the function set_include_path(). All this time, I defined a constant in the config.php file

define(\'BASE_PATH\', \'/var/www/mywebsite/public_html/         


        
2条回答
  •  失恋的感觉
    2021-02-20 16:45

    Using set_include_path() (or ini_set('include_path', ...)) allows you to specify multiple folders that would contain your library code. For instance, if your application relies on a variety of different frameworks/libraries, e.g. PEAR and Zend FW, you might have something like,

    ini_set('include_path', '/usr/local/php/pear:/usr/local/php/zendfw');

    The disadvantage to that approach is that it will use whatever file it finds first; if you have a file called "Mailer.php" in more than one of your include paths, it will include the first one it finds, causing subtle bugs if that's not your intention. Good code organization typically resolves that issue. Also, include_path goes through the realpath cache (http://us2.php.net/realpath), which sometimes needs to be tweaked to get better performance depending on your setup.

    Both ways are fine, however using the define() method is more explicit.

    FWIW, I generally use ini_set('include_path', ...).

提交回复
热议问题