Permission Denied in the PHP Destructor and the Current Folder

I learned something new today, so I’m going to share two cool things with you.

  1. File access is screwed up during a destructor call in PHP. I will show you how to get around this.
  2. How to create a universal absolute path variable that refers to the current folder you are in.

For the application I was working on today, we needed to add logging. This was a quick and dirty log that would (append) dump whatever was on the page into a file. I was working in a framework I wrote, where each page is actually a class. Neat, but not really necessary to understand the problem.

Here’s the code:

function __destruct() {
    $handle = fopen(CURRENT FOLDER . ‘no_sync.response.log’, ‘a’);
    fwrite($handle, ‘– START — (v.’ . self::VERSION . ‘ ‘ . date(‘Y-m-d H:i:s’) . “)\n”);
    fwrite($handle, ob_get_contents() . “\n– END –\n\n\n”);

Pay attention to that highlighted part and I’ll address it in a moment.

This code takes whatever is in the output buffer (which ultimately gets shown to the user) and chucks it in a file. Cool.

So how does it execute? I don’t call it explicitly. The __destruct is a special PHP function that gets automatically called when an instance of a class is about to get erased. It is rarely used by novice programmers because of its cryptic nature. But this is a good example where you might. 

Since it triggers when the class instance gets destroyed, I just need to unset the variable that contains the instance. Near the bottom of my code I added:

unset($FRAMEWORK); // my class is a property inside this variable!

And, as expected, everything worked. Yes, second try! Wait, second??

This isn’t how the code looked when I tried it the first time. That highlighted portion, “CURRENT_FOLDER”, was missing. If I’m writing to the current folder, I shouldn’t have to specify a folder path… Usually. This failed, even though the same exact code in the constructor worked fine.

It turns out that when you try to do file-stuff during the destructor, PHP is no longer in the original folder you started out in. PHP has “forgotten” where your script is and has reverted to the root folder (AKA “/” or “C:\”). This, in most systems, causes permission issues.

To get around this problem, you should be explicit about where you want to write the file. You can’t use relative paths (such as “…/logs/”). The “CURRENT_FOLDER” in my example happens to have the following value:

dirname($_SERVER[‘SCRIPT_FILENAME’]) . ‘/’

The great part about this is that it is not relative. It will be the fully qualified path to the folder the script was run from.

Note: from the command line, you should always type in the full path to the script. Failure to do so will render PHP unable to figure out an absolute path to your script. Just a tip for you people writing cron scripts. 😉

I hope that helps!

Lasik: 28 Things to Expect

Recently, I had the Lasik procedure done. My vision is great, and I am very happy I did it. If you’ve ever wondered what it’s like, here’s my everyday-Joe summary:


  1. I went in for consultation on a Saturday. The following Saturday, I underwent surgery. This short time frame is not unusual.
  2. The first visit is a relatively thorough eye exam (air puffs and everything). Surgery day involves more eye examinations.
  3. Before the surgery, they gave me some Valium for the “anxiety.”
  4. They numb your eye using eye drops. It makes your eyes feel a little dry and uncomfortable.
  5. They also dilate your eyes using drops.


  1. You are awake during the operation!
  2. You must keep your eye open for the entire duration of the operation, which is not difficult considering your eyes are numb.
  3. There is a dull pain when the doctor cuts your eye (yes, a human does this part). The lasers don’t hurt.
  4. When your cornea is pealed back, your vision is absolutely dismal.
  5. I keep hearing burning eye smells like squid. It did smell like burning something, but I wouldn’t say squid.
  6. It was a little difficult staring at the red dot because your vision temporarily goes black when the doctor cuts your eye. I think this was due to the pressure on the eye as he holds it down.
  7. The “laser” portion takes about 1 minute after the eye is cut open.
  8. After the lasers are done, the doctor puts your cornea flap back in place using tweezers. You’ll immediately notice a difference in your vision (it’s great!).
  9. Each eye is done one at a time. While one is being cut, the other is taped shut.
  10. The second eye hurts more than the first. The doctor even warned me. I think it’s because you know what’s coming. I was also definitely more nervous during the second eye.

After the Surgery (first two weeks)

  1. Your vision should be improved almost immediately after you walk out of the operation. Yes, you will be walking, but, no, you shouldn’t drive (see next two points).
  2. Your eyes will hurt a lot the first 48 hours. It will be a sharp burning pain like when you dip your eye in spicy sauce. You are given numbing eye drops for this (they work for about 20 minutes).
  3. Your eyes will be very dilated. Everything is vibrant, bright, and headache inducing. It was kinda cool, but it is nearly unbearable to be outside even with sunglasses.
  4. You will need to wear goggles to sleep for the first week. This sucked. Make sure you have a nice soft pillow so the goggles don’t smash into your face too hard.
  5. As soon as you get home, just go to sleep. They recommend you sleep as much as you can the day of the surgery. Sleeping is best anyway since the burning sensation will be at its worst immediately after the surgery.
  6. No, you can’t see any marks on your eyes.
  7. You are told to avoid water in your eye for the first two weeks. I accidentally tested this and got some in my eye during a shower. Yup, it burned.
  8. Your eyes will feel dry. Constantly. It is annoying. They will give you drops for this. It mostly goes away after two weeks. This will correlate with your vision quality.
  9. Your night vision will decrease, especially early on. Here’s what I observed: the dryness causes a haze, which fogs up your vision near light sources. Since light sources fuzz up your vision and mask darker areas, seeing things in the dark becomes very hard. This haze is largely unnoticeable in daylight.

After the Surgery (Long term)

  1. Your vision goes from very good to very fuzzy for the next 24 hours. This will continue, but not as erratically, over the next few weeks.
  2. You still can’t rub your eye (I am told up to 2 months).
  3. Water from showers are okay after the second week. I have not tested if I can swim yet.
  4. Follow up or corrective surgery is usually free or very (very) cheap.

Obviously this experience won’t speak for everybody. I have two friends who got Lasik. One has perfect vision and loves it. One is complaining that his vision still isn’t perfect. We all had the same doctor.