Why does getcwd () return / in __destruct ()?

I just noticed that getcwd () returns "/" if called in the __destruct () magic function, and in any other method it returns the expected path.

Do you have an explanation?

+5
source share
2 answers

This is the behavior of SAPI

"The destructors that are called when the script completes are already sent with HTTP headers. The working directory during the script completion phase may be different from some SAPIs (like Apache)."

From http://php.net/manual/en/language.oop5.decon.php

But, as mentioned in other answers, there are many ways to get the current relative path. If you changed it at run time, be sure to check somewhere inside the object.

+2

.

getcwd() __FILE__?

+2

All Articles