views:

57

answers:

2

Hello 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

+1  A: 

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.

e4c5
thanks a lot.if i use this in functions.php that will be in includes/functions.phpshould i load my libraries like "/includes" or "../includes" ?? that's still depending on the requesting script right ?
From.ME.to.YOU
Even the most experienced php programmers get confused on whether to use './filename' or '../filename' specially when two files might have a common include. The best way to overcome this is to put your stuff in directory that is on the include path - or to change the include path to point to the directory where the files are.you might also consider using include_once() or require_once() instead of include() or require()
e4c5
A: 

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.

TRiG