WICOM
 All Data Structures Namespaces Files Functions Variables Pages
Functions | Variables
import_functions.php File Reference

Functions

 load ($module, $path=null)
 

Variables

 $current_path = "./"
 

Function Documentation

load (   $module,
  $path = null 
)

This is a function for loading a module relative to the current file in static context.

Problem

There is a problem about paths. When an HTTP is requested on http://localhost/folderA/index.php the current path is the real one matching folderA virtual one, say ./proyect/folderA/index.php.

If this index.php requires the file ./proyect/folderA/folderB/require1.php , require1.php is a module that uses the folderA path as current directory, not folderB, and it may require something from other folder like ./proyect/folderA/folderC/require2.php so, it will have:

require_once '../folderC/require2.php';

This won't work... Because the current directory is the one matching the HTTP request virtual folder: ./proyect/folderA/ .

This is worse when require1.php is required by other PHP web page from other folder: If an HTTP request the http://localhost/index2.php and this index2.php requires ./project/folderA/folderB/require1.php, but require1.php wants the file on ../folderC/require2.php, wich will points to ./proyect/../folderC/require2.php giving up with an error.

Solution

A mechanism that concatenates relative paths:

  • When ./project/folderA/index.php executes load("require1.php", "folderB/") the current path should change to: "./folderB/".
    • When require1.php executes load("require2.php", "../folderC/") the current paht should change to: "./folderB/../folderC/"
      • When require2.php finish, the current path should be restored to: "./folderB/" deleting what require1.php stored when loading this file.
    • When require1.php finish, the current path should be restored to : "./" deleting what index.php stored when loading this file.

Variable Documentation

$current_path = "./"