2

I have a function that runs some fairly generic code that does a lot of work connection to a database and setting up different configuration variables. I have within a couple of if statements of this top-level function code that I run that is actually different from function to function.

This is how it looks now.

function get_users(){
  // config
  // set application keys
  // connect to database
  // retrieve user data
  // authentication to foreign api
  if(something){
    // some more red-tape
    if(something){
      //more more
      if(something){
        /* finally the good stuff */

        // the code here varies from function to function
        // eg. get users
        // probably will run: inner_get_users();

      }
    }
  }
}

function get_data(){
  // config
  // set application keys
  // connect to database
  // retrieve user data
  // authentication to foreign api
  if(something){
    // some more red-tape
    if(something){
      //more more
      if(something){
        /* finally the good stuff */

        // the code here varies from function to function
        // eg. get data
        // probably will run: inner_get_data();

      }
    }
  }
}

How I want it to work, perhaps using anonymous functions:

function instance($inner){
  // config
  // set application keys
  // connect to database
  // retrieve user data
  // authentication to foreign api
  if(something){
    // some more red-tape
    if(something){
      //more more
      if(something){
        /* finally the good stuff */

        Call inner

      }
    }
  }
}

function get_data(){

  instance(function(
    // get the data
  ));

}

or maybe

function get_users(){

  $var = function(
    // get the users
  );

  instance($var);

}

I'm looking for better, dryer, and more maintainable code.

1
  • you might be looking for a callback call_user_func? Commented May 21, 2012 at 13:53

1 Answer 1

1

This is something which PHP calls variable functions. This works both when $inner is the string name of a function and when it's an anonymous function (although the manual page for variable functions doesn't explain that).

function instance($inner){
  // config
  // set application keys
  // connect to database
  // retrieve user data
  // authentication to foreign api
  if(something){
    // some more red-tape
    if(something){
      //more more
      if(something){
        /* finally the good stuff */

        return $inner();

      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

-1 You always want to check if function exists before calling it.
iambriansreed, no, not always. It depends on how you handle errors and your approach to defensive coding. I prefer aggressive coding where you let things fail if you've done something wrong since it's much easier to troubleshoot that way.
"It's better to ask for forgiveness than permission", @EmilVikström this is a very aggressive coding style, maybe a try-catch block to catch any exception instead use always the set_error_handler function give us more flexibility on coding IMHO.
Yago, I've not coded PHP in a while now, but this should raise a catchable error, shouldn't it? In that case, catch the error where you can handle it in whatever way you wish, but don't catch it in the library function (I consider instance to be some sort of library function here). For comparison, note that defensive programming is often frowned upon in the Erlang community: erlang.org/faq/academic.html#id56015
Note that I'm not against catching errors. I'm just saying that you shouldn't code defensively. With that I mean that you don't need to check your pre-conditions everywhere, but let the small parts of your code fail if the pre-conditions gives you trouble. In the application at large, catch and log all errors (preferrably in a single location) and tell the user that something went wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.