Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define my own BASE_PATH vs. set_include_path?

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

And in all subsequent php files, I would include like so

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

Is there any advantage with the constant approach vs. the set_include_path approach and vice versa? Is the constant approach obsolete?

like image 451
John Avatar asked Nov 24 '09 03:11

John


People also ask

What is the difference between context path and base path?

Setting Property in application.properties Older and new versions of spring boot support in doing our own base path using configurations file that is application.properties. The main difference is path is called a base path in 1.x and context path in 2.x but the meaning of both is the same. Both of these change to proceeding with "/api/v1". 3.

Where are the include paths defined?

Where are the include paths defined? The include paths are defined in the "includePath" setting in a file called c_cpp_properties.json located in the.vscode directory in the opened folder.

How do I set an include path in Visual Studio?

To set an include path you now must right-click a project and go to: Is this answer outdated? In Solution Explorer (a palette-window of the VisualStudio-mainwindow), open the shortcut menu for the project and choose Properties, and then in the left pane of the Property Pages dialog box, expand Configuration Properties and select VC++ Directories.

How do I set the include path for a PHP file?

In order to use .htaccess files to set the include path, PHP must be installed as an Apache module. If PHP is compiled as a CGI binary, you can set the include path in a custom php.ini file (if, for example, you're being hosted somewhere and don't have access to the main php.ini file.


2 Answers

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

like image 85
Michael Avatar answered Oct 01 '22 15:10

Michael


I think Micahel's explanation is very clear.

I recommended you to use "set_include_path" when you store all your PHP files in an folder, for example: "libs/" (its easier). Using the define() method should be faster as you are specifying the file path explicitly.

Always try to avoid to use absolute paths unless they are really necessary. I found very useful to specify your paths this way:

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

This way you will avoid to have to update the path each time you move the code.

like image 42
lepe Avatar answered Oct 01 '22 15:10

lepe