English 中文(简体)
重新使用购买力平价功能以减少编码重复
原标题:Reusing PHP function to reduce code duplication
  • 时间:2012-01-13 16:24:38
  •  标签:
  • php
  • 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 ;

?>

我试图在我的<代码>中点燃——目前——数据(

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

然而,我收到以下错误:

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

页: 1

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

而用URL为它所投入的饲料,这将阻止我重新使用该守则。 我试图做的是不可能做的事,还是我的辛迪加不正确吗?

This isset method works fine where used elsewhere like for example the $tabTitle and $pageIntroductionHeading variables just need for the RSS feed.

提前感谢。

最佳回答

问题如下:

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

必须:

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

当你称职时,你还必须通过<条码>(源代码/代码)作为职能参数,例如:

get_current_weather_data($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);

提炼成本 like:

function get_current_weather_data() {
    global $sourceFeed

页: 1

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)将永远退回伪造。 你们正在当地范围内加以利用,而你们则在全球范围加以界定。 可在

我确信,你在那里所做的事情甚至不会 par。 它肯定在PHP5.2中赢得 par。

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

This is not valid. You cannot place an if statement inside a call to a function, and even if that did work, it would not affect the way the function is called.

你可以使用一种永恒的表述:

$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;

}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签