Я пытаюсь повысить читабельность моего кода, создавая дополнительные функции, но разделение функции get_current_weather_data () вызывает следующую ошибку:
Fatal error: Call to a member function children() on a non-object in C:\xampp\htdocs\Twinz\src\weather.php on line 37
Строка 37 должна быть последней фигурной скобкой после возвратаПогода;и эта ошибка произошла только после создания отдельной функции для get_city ().Я могу переместить это достаточно легко, но я хочу знать:
Разве нельзя разделить эту функцию и каково общее мнение.Что я пытаюсь сделать бессмысленно?
Эта функция загружает погодный фид Yahoo XML, который я обрабатываю и выводю необходимые данные;что происходит, город, температура и текущие условия.
<?php
function get_current_weather_data() {
// Get XML data from source
$feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f");
// Check to ensure the feed exists
if (!$feed) {
die('Weather not found! Check feed URL');
}
$xml = new SimpleXmlElement($feed);
get_city();
// Pull temperature
$weather['temp'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->temp;
echo $weather['temp'] . "<br />";
// Pull current conditions
$weather['conditions'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->text;
echo $weather['conditions'] . "<br />";
return $weather;
}
Функция get_city ():
function get_city() {
// Pull city
$weather['city'] = $xml->channel->children('yweather', TRUE)->location->attributes()->city;
echo $weather['city'] . "<br />";
}