1

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

trrrrrrm
  • 11,362
  • 25
  • 85
  • 130

3 Answers3

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.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • thanks a lot. if i use this in functions.php that will be in includes/functions.php should i load my libraries like "/includes" or "../includes" ?? that's still depending on the requesting script right ? – trrrrrrm Feb 10 '10 at 06:47
  • 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 Feb 11 '10 at 03:13
0

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
  • 10,148
  • 7
  • 57
  • 107
0

Check this library: https://github.com/FrederickMontiel/SimpleRouter-php

include_once __DIR__."/vendor/autoload.php";

use EasyProjects\SimpleRouter\Router as Router;

$router = new Router();
/*
    Require all files from folder to final subfolder
    
    Then if you have:
    app
        controllers
            - UsersController.php
            Implicits
                - UserImplicit.php
        models
            - UsersModel.php
        roots
            - UsersRoot.php
    
    Require all files php from controllers, implicits, models and roots
*/
$router->importAll("./app");

/*
    Require all files from folder
    
    If you have:
    app
        controllers
            - UsersController.php
            Implicits
                - UserImplicit.php
        models
            - UsersModel.php
        roots
            - UsersRoot.php
        - home.php
    
    Require all files php in app, only home.php
*/
$router->import("./configs");