01jayss
01jayss

Reputation: 1449

Optimize this PHP Code?

I am currently working on a mashup that incorporates many data feeds. In order to display ALL of the feeds that the user wants on one page, I am currently using if statements to cross-check with the MySQL database like this:

if($var["type"]=="weather") 

$var being the result of a call to mysqli_fetch_array

and then including code relevant to the function (e.g. weather) underneath, and then another "if" statement for another feed, so on so on. The problem is that there will be many feeds, and having all these "if" statements will be slow and redundant.

Is there any way to optimize this PHP code?

Upvotes: 1

Views: 170

Answers (7)

Toby Allen
Toby Allen

Reputation: 11213

Using either an If Statement or a Switch statement will be faster than you care about. It might look ugly and be cumbersome to maintain but it will be fast.

Upvotes: 0

Simone
Simone

Reputation: 2311

Try this:

$methods = array(
    "weather" => function() {
        // code
    },

    "otheroption" => function() {
    }
);

Just use then $var["type"] as a index in the array to get the function:

$methods[$var["type"]]();

You can obviuosly, for better readbility do something similar:

$methods = array(
    "weather" => "wheater_function",

    "otheroption" => "other_function"
);

and then call the functions this way:

call_user_func($methods[$var["type"]]);

To be even more object oriented we can obviously store in the array objects implementing a particular interface, or store object redifining the __call() magic method and use it like functions.

Upvotes: 1

Mchl
Mchl

Reputation: 62359

Polymorphysm for the rescue.

inteface FeedInterface {
  public function retrieve($params);
}

class FeedWeather implements FeedInterface {

  public function retrieve($params) {
    //retrieve logic for weather feed
  }

}

class FeedSports implements FeedInterface {

  public function retrieve($params) {
    //retrieve logic for sports feed
  }

}

With use of PHP class autoloading, each of above declarations can be in a separate file, possibly namespaced as well. Then your feed retrieval code could look like this:

$class = 'Feed'.$var["type"];
$feed = new $class;

$feed->retrieve($params);

That's overly simplified and would need some additional code for error handling, discovery of non-existing classes and such, but the idea should be clear.

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55334

Create an array that associates a function to each feed type:

$actions = array("weather" => "getWeather",
                 "news"    => "getNews");

Then use call_user_func to call the correct one:

call_user_func($actions[$var["type"]]);

Upvotes: 0

user927982
user927982

Reputation:

Another solution might be to map the "type" to a custom function using associative arrays.

e.g. (pseudo code)

function handle_wheater_logic() {
   // ... your code goes here
}

function handle_news_logic() {
  // .. your code goes here
}


$customFunctions = array("wheater" => "handle_wheater_logic", "news" => "handle_news_logic");

while ($row = mysql_fetch_...) {
    call_user_func ($customFunctions[$row["type"]])
}

This would eliminate the need to use a lot of if statements. You might as well do the "type to function" mapping in a configuration file or maybe just store the name of the custom function to call for each "type" in a database table - that's up to you.

You can, of course also pass parameters to custom function. Just checkout the documentation for call_user_func[_array].

Upvotes: 2

rzetterberg
rzetterberg

Reputation: 10268

A good solution for eliminating a lot of if statements and a huge switch statement just checking for one condition, would be to implement a design pattern such as the Strategy pattern.

This way you will have the code for each type separated, which makes it easier to overview and manage.

Here's an example of an implementation http://blogs.microsoft.co.il/blogs/gilf/archive/2009/11/22/applying-strategy-pattern-instead-of-using-switch-statements.aspx

Even if you won't implement this strictly it will give you some ideas on how to solve this elegantly.

Upvotes: 0

moteutsch
moteutsch

Reputation: 3831

You can use a switch statement.

Upvotes: 0

Related Questions