Tutorial :how to declare all your includes in one file that will be loaded in all project in PHP



Question:

i'm working on a project but, i'm sick of loading all the libraries in every single page

my project directories are something like this

|---x (( PHP PAGES ))    |--- x1.php (( PHP FILE ))    |--- x2.php (( PHP FILE ))  |---y (( PHP PAGES ))    |--- y1.php (( PHP FILE ))    |--- y2.php (( PHP FILE ))  |---includes (( LIBRARIES ))  a.php (( PHP FILE ))  b.php (( PHP FILE ))  c.php (( PHP FILE ))  

i tried to load all my libraries in a single file and load that file in all pages, but the problem comes up from the directories like for example loading libraries in x1.php will be something like this "../include/", but loading in a.php just needs "include/" i know there is a way to reach the "includes" directory no matter from where the request comes from but i have no clue how to do this.

Thanks


Solution:1

Loading each and every single file as an include will add unwanted bloat and possibly slow down your site. What you might try is class autoloading: http://www.php.net/manual/en/language.oop5.autoload.php

It eliminates the need to scatter includes all over the place but only the files you really need will be included.


Solution:2

The answer to your question is to set an include_path. By default, PHP looks for included files in the same directory as the calling file, but if you set an include_path it'll look there instead.

However, class autoloading and the __autoload() function are even better in the long run, so go with e4c5's answer.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »