debuggable

 
Contact Us
 
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19

Restarting a command line PHP script

Posted on 3/2/09 by Felix Geisendörfer

Hey folks,

ever wrote a PHP script that acts as a daemon? No? Read Kevin's excellent post on the topic as well as this one explaining the low-level plumbing.

If you already have, you might have run into the scenario where you want the script to restart itself. For example you might have changed the code of the script after it started running, and you want to replace the current process with one running the new code.

After a bit of trial and error, I present my glorious hack:

die(exec(join(' ', $GLOBALS['argv']) . ' > /dev/null &'));

Feel free to share your own war stories and suggestions regarding PHP scripts acting as daemons : ).

-- Felix Geisendörfer aka the_undefined

 

CakePHP Meetup this Friday in Berlin

Posted on 29/1/09 by Tim Koschützki

Hey folks,

as we have announced a week ago there will be a meetup of fellow Cakers in Berlin tomorrow. Here are the details:

Location:

Amaretto Due
Rigaer Strasse 30 (Ecke Gabelsberger Str.)
Check on Google Maps
http://www.amaretto-due.com/

Time

8pm GMT + 1 (German time).

Why this bar

It's the bar we always go to. Food there is good and so are the prices (Cocktails 3EUR each).

How do I find you?

Felix and I are tall men who will wear pink socks with small red hearts on the shoes. There will be some beer on the table. You cannot miss us.

What to bring?

Fun and the will to talk about what you are currently doing with CakePHP and what you plan to do. :) You can also bring your laptop but we've opted for a no-wifi location so we can focus on talking not surfing!

-- Tim Koschuetzki aka DarkAngelBGE

 

Donate your PHP arrays!

Posted on 29/1/09 by Felix Geisendörfer

Today I needed a list of mime types and extensions to determine the proper extension to assign to a file with a certain mime type. It wasn't difficult to find one.

However, as always when looking for tables and stuff like this on the web, I had to convert the data for using it in PHP. Usually I'd use my text editor and a few regex for the job.

But not today. Today was the last time I wasted energy on importing trivial data sets into PHP.

Why? Well, because I believe there is a fortune lost in man hours in the process on a daily basis. It is time for stopping that.

It is time for a new project called php arrays. (Warning: don't open anything but extensions.php / mime_types.php in your browser, it may freeze due to client-js syntax highlighting. Click the download link of the whole repo instead.).

Basically it is just a collection of PHP arrays, each containing one big array. So far I have done a little work in making two generators that currently produce the following arrays: big_cities.php (population > 15000), countries.php, extensions.php, mime_types.php and time_zones.php. Whenever I need to work with other stuff I'll add it there as well.

Now it is time for you to join the fun and share PHP arrays you generated or aggregated in the past. Fork the project on GitHub or send me a link to your array paste.

-- Felix Geisendörfer aka the_undefined

PS: What other arrays would like to see added to the collection in future?

 

Suppressing suppressing PHP errors with emptiness

Posted on 28/1/09 by Felix Geisendörfer

PHP's language constructors that disguise as functions are a bitch! I didn't know that empty() does not throw errors when accessing non-existing array keys, but it's actually in the manual. Thanks to everybody who pointed it out!

So please consider my previous post garbage as far as the actual example is concerned. The proper solution is:

if (!empty($step['options']['merge'])) {
  // do stuff
}

However, my threat of eventually using @ and breaking with other "best practices" remains. I will use whatever solution solves more problems for me then it creates ; ).

--Felix Geisendörfer aka the_undefined

 

Suppressing PHP errors for fun and profit

Posted on 28/1/09 by Felix Geisendörfer

Update: The conclusions below are wrong, empty() is the best solution to the particular problem, read the follow up post for more information.

As of late I am getting sick of some best practices I have taught myself. Never using the @-error suppressing operator quickly moving to the top of the list.

Before you start crying out loud (I know you will), let me say this: I do not mean to encourage anybody to use the @-operator. Applying the practice herein introduced may result in permanent damage to your coding habits and could serve as a gateway behavior to writing shitty code.

Ok, you have been warned, lets move on. Today I found myself writing the following line of code:

if (isset($step['options']['merge']) && $step['options']['merge']) {
  // do stuff
}

I was basically checking for a sub-key of an element to be true(ish). However, I knew this sub-key may not be included in the array under some circumstances, so a direct check for it could result in a PHP warning.

You can probably feel the pain of conflict here. On one side I want to write very short and expressive code. On the other side I am also trying to follow the so called "best practices". It is impossible.

Of course there is always the option to normalize the array before doing the check:

$step['options'] = array('merge' => false) + $step['options'];
if ($step['options']['merge']) {
  // do stuff
}

This however still adds a basically useless line of code. Plus normalizing a multi-dimensional array like this requires a more sophisticated merging algorithm than PHP provides you with (CakePHP's Set::merge() function provides good multi-dimensional array merging, but that would be throwing even more code and CPU cycles at the problem).

So today I have, for probably first time in years, I wrote the following line of code:

if (@$step['options']['merge']) {
  // do stuff
}

It still causes me mild pain to look at. I hope for that to go away soon. But I no longer have the strength to close my eyes to the very true lesson to be learned here: Using the @-operator like this solves more problems for me than it creates.

This may not be true for you. Using the @-operator is 4x slower than solution #1 and 1.3x slower than #2. If you run a loop with 100000 records, this is 150ms wasted (on my laptop, your mileage may vary). However, my scenario is more like running into this 5-10 times for any given request. So we are talking about 0.0015ms here.

It is not like I don't care about my CPU-cycle footprint. The opposite is true. By sparing myself the pain of typing up and maintaing more verbose-than-necessary code I can save millions of CPU cycles elsewhere! I can go and optimize stuff that actually needs to be optimized.

How will you choose?

-- Felix Geisendörfer aka the_undefined

PS: In case you wonder, @-suppressed errors do not show up in any error logs. So no problems with that.

 
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19