Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

getcwd

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?

like image 959
Michael Avatar asked Oct 06 '22 03:10

Michael


1 Answers

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.

like image 110
Danilo Kobold Avatar answered Oct 09 '22 21:10

Danilo Kobold