开发者

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?


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].


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.


You can use a switch statement.


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.


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"]]);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜