简体   繁体   English

require_once路径/到/file.php与file.php

[英]require_once path/to/file.php vs file.php

I'm having the following issue when including a file that includes another file: 包含包含另一个文件的文件时出现以下问题:

// path: /ajax/request.php
require_once("../classes/obj/Car.php");


// path: /classes/obj/Car.php;
require_once("Vehicle.php"); // superclass,  this works
require_once("../exception/NoWheelException");  // this doesn't work when Car.php is require_once()'d from /ajax/request.php

the problem seems to be that if a file A requires a file B that requires files C and D , it only works if C and D are used without "../" , or "./" previous to the file name, when using require_once() . 问题似乎是,如果文件A需要文件B ,文件B需要文件CD ,则在使用时,仅当CD在文件名前使用"../""./"情况下才有效require_once()

Is there any workaround or any ideas to fix this? 有任何解决方法或任何想法可以解决此问题?

Use __FILE__ so you know where it's being included from: 使用__FILE__以便从以下位置知道它的包含位置:

require_once(dirname(__FILE__) . '/../exception/NoWheelException');

Where: 哪里:

__FILE__ == "/classes/obj/Car.php";
dirname(__FILE__) == "/classes/obj";

Also, a note I didn't know - if your version of PHP is new enough, __DIR__ appears to be equivalent to dirname(__FILE__) 另外,我不知道一个注释-如果您的PHP版本足够新, __DIR__似乎等效于dirname(__FILE__)

More info on __FILE__ and other magic constants here 有关__FILE__和其他魔术常数的更多信息,请__FILE__此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM