5

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

Do you have an explanation for this?

Michael
  • 4,376
  • 10
  • 42
  • 66

2 Answers2

2

There is an old bug.

Instead of getcwd(), could you use __FILE__?

alex
  • 438,662
  • 188
  • 837
  • 957
2

It's a SAPI behaivor

"Destructors called during the script shutdown have HTTP headers already sent. The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache)."

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

But as mentioned in other answers there are plenty ways to get the current relative path. If you changed it during runtime make sure to note somewhere inside the object.

Danilo Kobold
  • 2,440
  • 1
  • 18
  • 30
  • I think it could really be a bug while there is an old one suggested by alex and icktoofay could't reproduce the problem on his newer PHP version. – Michael Feb 04 '13 at 00:47
  • It was a bug in 2005. now its an sapi problem. https://bugs.php.net/bug.php?id=31570 – Danilo Kobold Feb 04 '13 at 00:49