重用PHP函数以减少代码重复


Reusing PHP function to reduce code duplication

我正在从雅虎的天气RSS订阅源中获取伦敦和纽约的天气数据。我试图通过重用一个PHP文件来减少代码重复,该文件包含用于提取天气数据的函数。

下面是我正在调用的函数-get_current_weather_data()。该功能转到各种其他功能,例如get_city()get_temperature()功能。

<?php
function get_current_weather_data() {
// Get XML data from source
include_once 'index.php';  
$feed = file_get_contents(if (isset($sourceFeed)) { echo $sourceFeed; });
// Check to ensure the feed exists
if (!$feed) {
    die('Weather not found! Check feed URL');
}
$xml = new SimpleXmlElement($feed);
$weather = get_city($xml);
$weather = get_temperature($xml);
$weather = get_conditions($xml);
$weather = get_icon($xml);
return $weather;
}

在我的index.php中,我将RSS提要的URL设置为名为$sourceFeed的变量。

<?php
$tabTitle = ' | Home';
$pageIntroductionHeading = 'Welcome to our site';
$pageIntroductionContent = 'Twinz is a website which has been created to bring towns together! 
            Our goal is to bring communities around the world together, by providing 
            information about your home town and its twin town around the world. Our 
            site provides weather, news and background information for London and 
            one of its twin cities New York.'; 
$column1Heading = 'Current Weather for New York'; 
$column2Heading = 'Current Weather for London'; 
$column3Heading = 'Current Weather for Paris'; 
$sourceFeed = "http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f"; 
include_once 'header.php';
include_once 'navigationMenu.php';
include_once 'threeColumnContainer.php';
include_once 'footer.php';
?>

我尝试使用调用get_current_weather_data()函数中的提要

(if (isset($sourceFeed)) { echo $sourceFeed; }).  

然而,我收到以下错误

"Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in C:'xampp'htdocs'Twinz2'nyWeather.php on line 10
Weather not found! Check feed URL".

如果我更换

(if (isset($sourceFeed)) { echo $sourceFeed; }) 

使用提要的URL,它可以工作,但这将阻止我重用代码。我是在尝试做不可能的事情,还是我的语法不正确?

这种isset方法在其他地方使用时效果良好,例如$tabTitle$pageIntroductionHeading变量只需要RSS提要。

提前谢谢。

问题出现在以下行:

$feed = file_get_contents(if (isset($sourceFeed)) { echo $sourceFeed; });

必须是:

if(isset($sourceFeed)){ $feed = file_get_contents($sourceFeed); }

当你调用函数时,你还必须传递$sourceFeed作为函数参数,比如:

get_current_weather_data($sourceFeed);

您正试图访问函数中的全局变量$sourceFeed。将其作为参数传递给函数:

// Pass $sourceFeed as a function parameter:
function get_current_weather_data($sourceFeed) {
  // Get XML data from source
  include_once 'index.php';  
  $feed = file_get_contents($sourceFeed));
  // Check to ensure the feed exists
  if (!$feed) {
      die('Weather not found! Check feed URL');
  }
  $xml = new SimpleXmlElement($feed);
  $weather = get_city($xml);
  $weather = get_temperature($xml);
  $weather = get_conditions($xml);
  $weather = get_icon($xml);
  return $weather;
}

并将函数调用为:

$sourceFeed = "http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f"; 
$weather = get_current_weather_data($sourceFeed);

尝试使$sourceFeed全局化,如下所示:

function get_current_weather_data() {
    global $sourceFeed

get_current_weather_data($sourceFeed)

您的问题是一个可变范围的问题。进入函数内部的变量是一个仅用于函数的新变量。一旦函数返回/完成,它将不可访问。因此,您需要让函数知道值是什么:

 function get_current_weather_data( $sourceFeed ) {  // this tells the function to 
 // read that variable when it starts.  You also need to pass it when you call the function.
 get_current_weather_data( $sourceFeed );
 // OR
 get_current_weather_data( 'http://myurl.com' );

isset($sourceFeed)将始终返回false。您在本地范围中使用它,而在全局范围中定义它。有关变量作用域的详细信息,请访问http://tr2.php.net/manual/en/language.variables.scope.php.

我确信您在那里所做的事情甚至不会解析。它肯定不会在PHP 5.2中解析。

$feed = file_get_contents(if (isset($sourceFeed)) { echo $sourceFeed; });

这是无效的。不能在对函数的调用中放置if语句,即使这样做有效,也不会影响函数的调用方式。

您可以使用三元表达式:

$feed = file_get_contents((isset($sourceFeed)) ? $sourceFeed : '');

但即使这样也不会对您有所帮助,因为您必须将文件名传递给file_get_contents(),否则会出现您看到的错误。

您的方法完全错误,您不应该包含index.php来定义变量,而应该将变量作为参数传递给函数。例如:

function get_current_weather_data($sourceFeed) {
  // Get XML data from source
  $feed = file_get_contents($sourceFeed);
  // Check to ensure the feed exists
  if (!$feed) {
      die('Weather not found! Check feed URL');
  }
  $xml = new SimpleXmlElement($feed);
  $weather = get_city($xml);
  $weather = get_temperature($xml);
  $weather = get_conditions($xml);
  $weather = get_icon($xml);
  return $weather;
}