开发者

php's include_path for includes starting with ./

开发者 https://www.devze.com 2023-02-24 20:33 出处:网络
In my PHP framework, I want to use several functions of another PHP framework. That other framework has only one portal script (index.php). From there it does everything (bootstrap, call controllers a

In my PHP framework, I want to use several functions of another PHP framework. That other framework has only one portal script (index.php). From there it does everything (bootstrap, call controllers and actions etc). The other framework includes all its files starting with ./

The other framework's index.php:

include './inc/bootstrap.php';

In bootstrap.php:

include './inc/configs.php';
include './inc/database.php';

etc etc

So it looks like all the includes are relative to the folder index.php is in.

Is there any way to set up the environment so I can bootstrap the framework from another folder (somewhere within my framework, so a completely different folder and not the portal script)?

include_path includes 开发者_StackOverflow中文版. and I've tried it with the other framework's folder in the include_path as well but that didn't change anything.

I'm guessing it's the ./ includes, but I can't change those (the other framework isn't part of my framework and will be updated some time). Is there a way around them (or am I doing it plain wrong)?


path starting with . or / ignore the include_path because they are relative to the working directory.

So the only way is to change the working directory using the PHP function chdir:

In your framework:

chdir('/path/of/the/other/framework'); // change the working directory
require '/path/of/the/other/framework/bootstrap.php'; 

// optionally you can reset the working directory
chdir(dirname(__file__));


When you go to include the other framework's bootstrap file, you'll need to chdir() into that directory first, then you can include it, and all the subsequent includes it will do will be properly relative to the bootstrap file.


You should be able to do this with set_include_path( $path_to_include_files ). If you still have problems it might mean that there is another place in your script that is setting the include_path to another value.


The files are included in order of the inclusion path, in this example, the directory structure is as follows:

.
│— index.php
│— t1
│     │— a
│     └─ b
└─ t2
      |— b
     └─ c

<?php

set_include_path('t1' . PATH_SEPARATOR . 't2');

include 'a';  // includes from T1
include 'b';  // includes from T1
include 'c';  // includes from T2

?>

Note that the include path affects only include/require functions.

<?php

var_dump(file_exists('a'));  // false
var_dump(fopen('b', 'r'));  // file not found

?>

source

0

精彩评论

暂无评论...
验证码 换一张
取 消