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

帅比萌擦擦* 提交于 2020-01-02 05:11:26

问题


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?


回答1:


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.




回答2:


There is an old bug.

Instead of getcwd(), could you use __FILE__?



来源:https://stackoverflow.com/questions/14678947/why-does-getcwd-returns-in-destruct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!