Define your own BASE_PATH and set_include_path?

I found out about the set_include_path () function. All this time I defined a constant in the config.php file

define('BASE_PATH', '/var/www/mywebsite/public_html/'); 

And in all subsequent php files I would include so

 include(BASE_PATH.'header.php'); include(BASE_PATH.'class/cls.data_access_object.php'); 

Is there any advantage to a consistent approach to the set_include_path approach and vice versa? Is the ongoing approach obsolete?

+7
php
source share
2 answers

Using set_include_path () (or ini_set ('include_path', ...)) allows you to specify several folders that will contain your library code. For example, if your application relies on many different frameworks / libraries, for example. PEAR and Zend FW, you might have something like

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

The disadvantage of this approach is that it will use any file that it finds first; if you have a file called "Mailer.php" in more than one of your included paths, it will include the first one found, causing subtle errors if that is not your intention. Good code organization usually solves this problem. In addition, include_path goes through the cache of the real path ( http://us2.php.net/realpath ), which sometimes needs to be tuned to improve performance depending on your configuration.

Both methods are good, but using the define () method is more explicit.

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

+5
source share

I think Michael’s explanation is very clear.

I recommended you use "set_include_path" when saving all your PHP files to a folder, for example: "libs /" (this is easier). Using the define () method should be faster because you explicitly specify the file path.

Always try to avoid using absolute paths if they are not really needed. It was very useful for me to indicate your paths as follows:

 define("BASE_PATH", dirname(__FILE__)); 

This way you will avoid having to update the path every time you move the code.

+4
source share

All Articles