How to include PHP files in a compatible way for packaging with and without Phar?

Given a PHP application with the following structure:

/ lib/ mylib.php web/ index.php // includes "mylib.php" with: require_once __DIR__ . "/../lib/mylib.php" 

I am trying to cover the following cases simultaneously with the same source base:

  • No Phar : the ability to use the application as is, with DocumentRoot pointing to the website and redirecting all requests to index.php.
  • Minimum Phar : the ability to create a phar that contains only web / index.php and which will be saved as: web / application-minimal.phar.
  • Full Phar : the ability to create a phar that contains both the contents of the lib directory and web / index.php, and this will be saved as: web / application-full.phar.

In the case of phar files, all requests will be redirected to the phar file itself.

Is it possible to achieve all these use cases without changing the value of * require_once *?

I tried different approaches (relative / absolute) to enable lib / mylib.php from * web / index.php ", and also try some tricks with Phar :: mount (). None of my attempts have been successful.

+4
source share
1 answer

Just rely on your include path.

Do not execute:

 require_once __DIR__ . "/../lib/mylib.php"; 

a

 require_once "mylib.php"; 

and set the inclusion path at the beginning of your script:

 set_include_path(__DIR__ . '/../lib/' . PATH_SEPARATOR . get_include_path()); 
+1
source

All Articles