Increase cohesion by reusing a PHP function for multiple RSS feeds

240 views Asked by At

My homepage contains weather for three cities around the world as displayed in the image

index.php

In the home page I declare 3 variables storing the RSS URL for each city

$newYorkWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f';
$londonWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c';
$parisWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c';

I pull identical tags out of the three URL's displayed above and use 3 identical functions apart apart from the variable passed into it.

Below shows the variable being passed into the function. Obviously other functions are used before $weather can be returned.

function new_york_current_weather($newYorkWeatherSource) {

// Get XML data from source
if (isset($newYorkWeatherSource)) {
    $feed = file_get_contents($newYorkWeatherSource);
} else {
    echo 'Feed not found.  Check URL';
}

checkWeatherFeedExists($feed);
$xml = new SimpleXmlElement($feed);

$weather = get_dateTime($xml);
$weather = get_temperature_and_convert($xml);
$weather = get_conditions($xml);
$weather = get_icon($xml);

return $weather;
}

As I mentioned, I current repeat this function 3 times just replacing the $newYorkWeatherSource variable that is passed in the above example. Any ideas how I could reuse this function 3 times but yet pass in different URL to keep my homepage showing weather from the 3 cities? Ofcourse, it's easy to reuse the function if each city was represented on individual pages but the purpose is to keep them together for comparison.

Any ideas?

Thanks in advance.

1

There are 1 answers

2
Charles On BEST ANSWER

As I mentioned, I current repeat this function 3 times just replacing the $newYorkWeatherSource variable that is passed in the above example. Any ideas how I could reuse this function 3 times but yet pass in different URL to keep my homepage showing weather from the 3 cities?

Maybe I'm entirely missing the point of your question, but are you asking how to rename the function and variables? Because, if so, it's just a matter of search and replace on the first few lines of the function...

function get_current_weather($rss_url) {
    // Get XML data from source
    if (isset($rss_url)) {
        $feed = file_get_contents($rss_url);
    } else {
        echo 'Feed not found.  Check URL';
    }
    // ...

Simply replace the city-specific functions with one starting out like this, and call it three times, one time for each specific city RSS feed URL.


From the comments:

but I'm just wondering what I will do with the 3 RSS URL variables because I can't replace rename them all to $rss_url as I will just be overwriting them until eventually the only URL will be Paris

I believe you may be suffering from a misunderstanding about PHP variable scope. Let's take this snippet as an example:

function bark($dog) {
    echo 'The dog says ', $dog, ".\n";
}

$cat = 'meow';
bark($cat);

This code will emit The dog says meow. When you call the bark function with a variable, PHP takes a copy of the data* and passes it into the function as the variable name specified in the function. You don't need to name the variable the same thing both inside and outside. In fact, you can't** even see variables defined outside of a function:

function i_see_you() {
    echo 'The dog heard the cat say ', $cat, ".\n";
}
$cat = 'meow';
i_see_you();

This code will emit The dog heard the cat say ., as $cat is out of scope here.

Getting back to the problem at hand, we still have three weather URLs.

$newYorkWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f';
$londonWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c';
$parisWeatherSource = 'http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c';

All you need to do in order to make things work is:

echo get_current_weather($newYorkWeatherSource);
echo get_current_weather($londonWeatherSource);
echo get_current_weather($parisWeatherSource);

Inside the function, the proper variable with the proper name will have the proper data, and the right thing will happen.

*: PHP uses something called "copy-on-write", which does what you think it might do. It's completely safe to pass around variables containing large data. It will not consume unexpected amounts of memory. There's no need to use references. In fact, forget I ever said anything about references, you don't need them right now.
**: It's possible to see variables from the global scope by using the global keyword. Globals are bad practice and lead to spaghetti code. You might want to read more about variable scope in PHP.