Skip to main content

Posts

Goto and Exceptions

It slipped quietly under the radar for some. For others, it raised quite a stir. No, I'm not talking about PHP's implementation of namespaces (a battle that's finally done and over much to everyone's relief). I'm referring to the infamous goto statement. Many programmers have had the " goto is evil" mantra drilled into them from an early start. The basis of this can probably be traced back to Edsger Dijkstra's March 1968 letter to the editor in Communications of the ACM (though he wasn't the first to argue against goto ). Dijkstra felt the proliferation of goto at the time was producing unmaintainable "spaghetti" code. Fast-forward more than 40 years later and the controversial feature is still alive and well... and about to make its debut appearance in PHP. I found myself discussing some of the new features in PHP 5.3 with a friend a few days ago after he read my previous post about anonymous functions and closures . Our conversatio...

Anonymous Functions and Closures

Of all the new goodies that are promised in PHP 5.3, the two which I think I am the most excited about are anonymous functions and closures. Anonymous functions are functions that are defined without being bound to a proper name. Typically, anonymous functions are used only a limited number of times and for a specific purpose; you could think of them as "throw-away" functions if you'd like. Let's consider the following example which illustrates a standard function used as a callback: function percentVowels_callback($word) { $word = strtolower($word); $chars = count_chars($word); $numVowels = 0; foreach (array("a", "e", "i", "o", "u") as $vowel) { $numVowels += $chars[ord($vowel)]; } return $numVowels / strlen($word); } $animals = array("aardvark", "elephant", "iguana", "orangutan", "urchin"); $percentVowels = array_map("percentVo...

Evil Access

I was thinking today about database APIs when inspiration struck. I ended up hacking out the following class, which I think demonstrates a rather interesting approach to interfacing with a database (interesting enough at least to post here). class DBQuery implements Iterator { protected $_db; protected $_query; protected $_result; protected $_index; protected $_num_rows; public function __construct($host, $dbname, $username, $password) { $this->_db = new PDO("mysql:dbname=$dbname;host=$host", $username, $password); } public function __get($query) { $this->_query = $query; $this->_result = $this->_db->query($query); return $this->_num_rows = $this->_result->rowCount(); } public function quote($value) { return PDO::quote($value); } public function __call($query, $values) { $this->_query = $query; $this->_result = $...

Motivational Posters

I've been having a hell of a time fighting with some Perl code at work lately. I tossed together a humorous Perl demotivational poster to release some of my frustration, and thought others might appreciate it. But why stop there? There are other languages worth making fun of besides Perl! So, I tossed together some more snarky posters to be fair.

7 Things You Didn't Care to Know About Me

I've been tagged by Chris Cornutt and Jeff Jones to share some things you probably don't know about me. (It's nice to see chain mails flow through the blogosphere too, eh?) So, here's seven little-known facts about yours truly... My first career choice was to be a diocesan priest, and I briefly attended Seminary after I graduated from high school. My family isn't overly religious, and my parents didn't push me to go; I think I just like to help people and felt drawn to the rich history of the Roman Catholic Church. Life has a funny way of playing out differently than we intend for it, doesn't it? After I left seminary, I attended a state university and majored in elementary education with a concentration in music history and literature. I'm not overly fond of children, though, so I didn't complete the program. I would only need to take two more classes to be a state-certified elementary teacher. I studied French for 5 years in high school...

Happy New Year

I've never been one to make New Year's resolutions. If I really want to change something about myself or my life, why what until January 1st to set goals for myself? It doesn't matter to me if it's January 1st, April 15th, July 4th, or December 26th... I just set goals and go for them! Instead, I prefer to choose an over-reaching theme for the year. This suits me better because there's no success or failure benchmarks to meet; it's something I keep in mind to help guide me as I tackle the challenges and opportunities that come my way. 2006 was my "Year of Adventure." 2007 was my "Year of Change." 2008 was my "Year of Success." What will 2009 be? I've chosen to designate 2009 as a "Year of Balance" for myself. Like everyone, there's good things and bad things in my life; putting the balance back into my life again will empower me to better appreciate the good and deal with the bad more productively. So, it was ver...

Paste Ninja Goes Multilingual

Chris Cornutt was nice enough to give Paste Ninja (the premier PHP-powered pastebin app) some exposure by highlighting it on PHPDeveloper.org a week or so ago. Thanks, Chris! As a result of the extra publicity, the site saw an increase in traffic as people came to check it out. I hope they liked what they saw... but stay tuned, because there's more features to come! It was interesting to look through the access logs afterward. There are a lot of pastebins available on the Internet, but the feature set and usability of the application is what will help differentiate it from the rest. The list of countries from which various people visited and the values of their browser's Language-Accept headers peeked my interest, and I realized that I needed to offer Paste Ninja's interface in multiple languages so people could use my service in whichever language they may find the most comfortable. So, Paste Ninja is now multilingual! The goal is to translate PasteNinja's i...

PHP != Perl

People irk me when they expect PHP to behave like <insert your favorite language here>. Most recently, I ran across this blog entry Perl vs PHP: Variable Scoping via the help of PHPDeveloper.org . I'm sure Leonid is a great fellow and really smart... but he just happened to trip my irk-wire. The post is an innocent enough "here's a problem that I ran into, so I'll blog it in case someone else has the same issue." Reading between the lines though, I get the feeling his underlying belief is that because PHP and Perl are similar that they should have the same variable scoping rules, and that since PHP's doesn't correspond to Perl's that PHP is wrong and this should be documented as such. Each language is different. If PHP were so similar to Perl, it wouldn't be called PHP... it'd be called Perl. Or maybe a dialect, like PHPerl. I really wish people would stop comparing the two. Quite frankly, I don't see much similarity betwe...

JavaScript Keyboard Widget

Have you ever done something stupid just so you don't have to do whatever it is you're supposed to be doing because you feel like procrastinating?  I found myself doing that this evening by programming a keyboard widget in JavaScript. Right now it's just sloppy code without any real functionality, but feel free to take a look at it at   http://www.saltcitytech.com/projects/keyboard . Theoretically, one could add a couple of state variables to monitor the ctrl, shift, alt and caps lock keys, check those variables in the keys' onclick callback, and have a fully functional keyboard widget.  Since the key characters are defined in a mapping object, it should be relatively easy to internationalize it just by changing the mapping definitions. I may clean up the code in the future depending if someone actually sees a viable use for it or if I find myself needing to procrastinate again.

Dialog and Paste Ninja

I'd like to share with everyone two projects that I've been working on in what little spare time I've had lately-- Dialog and Paste Ninja. Dialog Dialog is a lightweight JavaScript-based dialog window. Why does the world need another dialog widget, you ask? It probably doesn't, but I couldn't find a freely available one that was as flexible as I wanted so I wrote my own. Flexibility and portability were key design features, so Dialog is written completely in JavaScript and CSS, and its appearance is fully CSS driven. Dialog supports multiple dialog types by default, such as confirmation, information, warning, and error, and can further be customized with a custom dialog type. It can even display modal dialogs and block input to the page until the prompt is dismissed! You can learn more about it and get the code at its project page, http://www.saltcitytech.com/projects/dialog from my JavaScript Experiments repository at GitHub. Paste Ninja Paste Ninja i...