问题是我file _ get _ contents("body.html")在与同一文件夹中的类中使用该方法body.html.问题是我收到错误,说找不到该文件.这是因为我从另一个类需要使用该方法的文件,file _ get _ contents("body.html")突然我必须使用"../body/body.html"作为文件路径..!
这有点奇怪吗?调用该方法的类与file _ get _ contents("body.html")它在同一个文件夹中body.html,但由于该类是其他类的其他类所必需的,我需要一个新的文件路径?!
这是目录和文件的简短列表:
lib/main/main.php
lib/body/body.php
lib/body/body.html
这是body.php:
class Body {
public function getOutput(){
return file_get_contents("body.html");
}
}
Run Code Online (Sandbox Code Playgroud)
这是main.php:
require '../body/body.php';
class Main {
private $title;
private $body;
function __construct() {
$this->body = new Body();
}
public function setTitle($title) {
$this->title = $title;
}
public function getOutput(){
//prints html with the body and other stuff..
}
}
$class = new Main();
$class->setTitle("Tittel");
echo $class->getOutput();
Run Code Online (Sandbox Code Playgroud)
我要求的是修复body.php与同一文件夹中的错误,body.html但是当另一个类需要body.php来自方法中的其他位置时我必须更改路径file _ get _ contents("body.html")
谢谢!
PHP基于文件的函数的范围始终从执行堆栈中的第一个文件开始.
如果index.php被请求,并且包括classes/Foo.php其中需要包括'body/body.php',则文件范围将是index.php.
基本上,当前的工作目录.
不过,你有一些选择.如果要在与当前文件相同的目录中包含/打开文件,可以执行以下操作
file_get_contents( dirname( __FILE__ ) . '/body.html' );
Run Code Online (Sandbox Code Playgroud)
或者,您可以在常量中定义基目录,并将其用于包含
define( 'APP_ROOT', '/path/to/app/root/' );
file_get_contents( APP_ROOT . 'lib/body/body.html' );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2819 次 |
| 最近记录: |