I use pcntl_signal
function to handle Ctrl+Z, Ctrl+C and exit at safe point from the application.
Can I do the same when user close window?
Try the hangup signal.
SIGHUP
You can use: http://en.wikipedia.org/wiki/Unix_signal
To find and overview of when each signal will be sent.
There no UI specific a specific "tab/window" closed signal and you would have to hook into X11 to accomplish anything like that (certainly not recommended especially in PHP) but SIGHUP is probably what you need.
There is a SIGCHLD but I don't know about anything that indicates that a parent is terminating which is what happens when you close the tab or window.