Requiring file on destruct
I have class and do like this:
function __destruct() {
$this->load_file('epicEndingfile.php');
}
And I get an error:
Warning: require(...) [function.require]: failed to open stream:开发者_StackOverflow社区 No such file or directory
And when I do the same with __construct
it works. Why is that?
EDIT: I actually don't require file, but I use method to do that.
require uses your CD (Current Directory), not the directory where you have your file put.
It might be changing in your application context (between construct and destruct), If you want to provide relative file paths based on your current file, use this :
require dirname(__FILE__)."/epicEndingfile.php";
On PHP 5.2 and below and
require __DIR__."/epicEndingfile.php";
On PHP 5.3+
You need to calculate the location of the file. The way you have it written, the file must exist in the same directory as the file that is calling the function. Add the full path and it should work.
require '/full/path/to/file/epicEndingfile.php';
In the __destruct method, first do an echo getcwd();
and you will see the current working directory at that stage, sure it was changed at that point.
For example, if your class is being defined at a file located in a different directory than your main script, the require will be relative to the class defining file.
A good idea is always to define a constant containing the base directory of your script. So somewhere in the first lines of your main php file, add a.
define('rootdir', dirname(__FILE__));
// you can replace dirname(__FILE__) with __DIR__ if it works in you PHP Version.
Then everytime you do an include or require of a file that is located relative to your main script file.
include rootdir. '/requires/include.php';
for example.
精彩评论