Mobile and web design development (Android, iPhone, WordPress)
Google+TwitterLinkedinFacebook
Subscribe
HomePHP, MySQL, Server sideFunction lookup tables in PHP

Function lookup tables in PHP

I’ve don my share of coding in Assembly language and one of the constructs that is pretty efficient to use is lookup tables for calling subroutines or functions. One of the things I like PHP is its ability to do the same thing.

For example, if you want to perform exception/error handling based on a status code, you can use lookup tables instead of using a switch() construct. If you use a switch() construct, you’ll have something like:

switch( $status ) {
    case 0: do_action_1(); break;
    case 0: do_action_1(); break;
    default: do_default();
}

If you use a lookup table, your source code will have something like:

$lookup = array( 'do_action_1','do_action_2');

if ( $status >= 0 &amp;&amp; $status < count($lookup) ) {
    call_user_func($lookup[$status]);
}

I’m sure there are other uses of lookup tables and the combination of using array() and call_user_func() in PHP offers a lot of flexibility in coding.

Leave a Reply

No related posts.

  • Hi, I'm Gerry. Welcome to my blog and sites about mobile, technology, The Pinoy, #crazyidea, games, WordPress, Ubuntu, Mac, iOS, Android, Symbian... I started sites like Ortigas Online and iMakati which focuses on local community information. Read more about Gerry
    Status (Twitter, Linkedin, Facebook...)

    Just found out that the Android-based HTC Sensation XL phone is now in the Philippines for about Php 28,000.

  • Visit my other sites

    • Gerry

      My blog about the Philippines, mobile, technology, games, mac, linux...

    • Apps & Games

      Reviews, news of mobile apps & games for Android, iOS, Blackberry, Symbian...

    • Connected Phone

      The computer is the mobile phone. The always connected phone evolution.

    • Pinoy Me

      Pinoy businesses, enterprises, startups, professionals all over the world

    • Java Midlet

      Java mobile apps & games - news, reviews, tips, mobile phone & devices

  • PINOI Sites

    Are you interested in putting up your own PINOI-enabled local community site? Contact me

    • PINOI

      Enabling local communities on the Internet

    • Makati

      iMakati.com is a site that provides information about Makati City.

    • Ortigas

      Community site that provides information about the Ortigas Center.