PHP set_inlude_path and include best practices

Where

set_include_path("../../");

      

point to?

I am trying to run some PHP code on a standard XAMPP server. And put my stuff in the htdocs folder. Inclusion points to relative paths, but that won't work. Are there best practices for inclusion? The code must run on multiple developers' machines.

0


source to share


2 answers


The relative path should work fine (warning: apache on windows, as I know, doesn't follow sim links or whatever on this os).

Perhaps you should use the syntax (to avoid problems with different php versions)

ini_set('include_path', 'yourdir');

      

and check the return value to make sure everything is ok.

Moving on to best practices: I need to set a config directive in a script, especially if it exists throughout the project, is wrong, or at least dangerous.



The best practice is to include the directive in the .htaccess file in the directory containing the php files project .

Better yet, faster, put the directives in the appropriate virtualhost section of your apache configurator. However, for development purposes, .htaccess is more flexible and therefore preferred.

It should be something like this: php_value include_path ".: ../ ..: <your path collection>"

By doing this, you can share php configuration without the ability to fiddle with the ini_set directive in every php file you write.

Not to mention, if you have a special file that requires a custom include_path, you can set it in the file and it will be obvious to everyone with a very quick glance.

+2


source


Two directories down from where the file (or the file that included it) started.

As far as best practices go, there are only two tips I can give you.



  • Use relative paths.
  • If relative paths are giving you problems, use the absolute path as one static variable and developers only change one variable.
+1


source







All Articles