Deprecated: Call-time pass-by-reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
getgraphics
Forum Newbie
Posts: 2
Joined: Mon Jun 28, 2010 11:12 pm

Deprecated: Call-time pass-by-reference

Post by getgraphics »

I am trying to display weather on my site and I am getting many error messages such as:

Deprecated: Call-time pass-by-reference has been deprecated in xmllib.php on line 63

How can I fix this? There are 3 files below that are being used 1) weather.php 2) config.php and 3) xmllib.php

I am trying to learn PHP, and any help for a newb would be greatly appreciated. Thanks...

______________
1) weather.php file

Code: Select all

<?php
###################################################################################
#
# Weather report 1.3 index.php, by Aid Arslanagic, version 0.3
# http://www.simpa.ba
#
# This code is released under The GNU General Public License (GPL).
# Read the license at http://www.opensource.org/licenses/gpl-license.php
#
###################################################################################
ini_set('allow_call_time_pass_reference', '1');

require ("inc/config.php"); 

$handle = fopen($query, "r");
$xml = '';
while (!feof($handle)) {
  $xml.= fread($handle, 8192);
}
fclose($handle);
$data = XML_unserialize($xml);

###################################################################################
# Change the layout for your site below
###################################################################################

###################################################################################
# Top
###################################################################################
echo "<html><head><title></title><link href=\"../css/weather.css\" rel=\"stylesheet\" type=\"text/css\" /></head><body leftmargin=\"0px\" topmargin=\"0px\" marginwidth=\"0px\" marginheight=\"0px\"><div id=\"mainframe\"><div id=\"currenttemp\">";
echo "Current Temperature<br><span id=\"tmptext\">Stratford-Upon-Avon</span></div>";


###################################################################################
# DAY 0
###################################################################################
echo "<div id=\"temp1\">";
echo "<div id=\"img\">";
echo "<IMG SRC='../images/icons/" . $data[weather][dayf][day][0][part][0][icon] . ".gif ' style=\"align:left;height:60px; width:60px;\"></div>";
echo "<div id=\"day\">Today";
echo "<br><span id=\"tmp\">";
echo $data[weather][cc][tmp];
echo "&deg;" . $data[weather][head][ut]  . "</span><br>" ;
echo "<span id=\"tmptext\">" . $data[weather][cc][t] . "</span>";
echo "</div></div>";

echo "<div id=\"temp1\">";
echo "<div id=\"img\">";
echo "<IMG SRC='../images/icons/" . $data[weather][dayf][day][1][part][0][icon] . ".gif ' style=\"align:left;height:60px; width:60px;\"></div>";
echo "<div id=\"day\">";
echo $data[weather][dayf][day]["1 attr"][t] ;
echo "<br><span id=\"tmp\">";
echo $data[weather][dayf][day][1][hi];
echo "&deg;" . $data[weather][head][ut]  . "</span><br>" ;
echo "<span id=\"tmptext\"></span>";
echo "</div></div>";

echo "<div id=\"temp1\">";
echo "<div id=\"img\">";
echo "<IMG SRC='../images/icons/" . $data[weather][dayf][day][2][part][0][icon] . ".gif ' style=\"align:left;height:60px; width:60px;\"></div>";
echo "<div id=\"day\">";
echo $data[weather][dayf][day]["2 attr"][t] ;
echo "<br><span id=\"tmp\">";
echo $data[weather][dayf][day][2][hi];
echo "&deg;" . $data[weather][head][ut]  . "</span><br>" ;
echo "<span id=\"tmptext\"></span>";
echo "</div></div>";




echo "</div></body></html>";

###################################################################################
# Uncomment this to see complete array:
#
#echo "<pre>";
#print_r($data);
#echo "</pre>";
###################################################################################
?>
__________________
2) config.php file

Code: Select all

<?php
###################################################################################
#
# Weather report 1.3 config.php, by Aid Arslanagic, version 0.3
# http://www.simpa.ba
#
# This code is released under The GNU General Public License (GPL).
# Read the license at http://www.opensource.org/licenses/gpl-license.php
#
###################################################################################
require ("xmllib.php");
###################################################################################
# XML Library, by Keith Devens, version 1.2
# http://keithdevens.com/software/phpxml
###################################################################################

$source = "http://xoap.weather.com/weather/local/";
$prod = "xoap";
###################################################################################
# Check your location at this address:
# http://xoap.weather.com/search/search?where=sarajevo # replace "sarajevo" with
# your city and enter your location here:
$code = "BKXX0004";
###################################################################################
$cc = "*";
###################################################################################
# You can change units from Metric "m" to Standard "s":
$unit = "m";
###################################################################################
# You can change number of days:
$dayf = "10";
###################################################################################
$par = "1005217190";
$key = "2e4490982af206e0";
# note that prod=xoap recently changed to link=xoap - thanks to Neal-at-fenna.co.uk
$query =  $source . $code . "?link=" . $prod . "&cc=" . $cc . "&dayf=" . $dayf . "&unit=" . $unit . "&par=" . $par . "&key=" . $key;
?>
____________
3) xmllib.php file

Code: Select all

<?php
###################################################################################
#
# XML Library, by Keith Devens, version 1.2
# http://keithdevens.com/software/phpxml
#
# This code is Open Source, released under terms similar to the Artistic License.
# Read the license at http://keithdevens.com/software/license
#
###################################################################################

###################################################################################
# XML_unserialize: takes raw XML as a parameter (a string)
# and returns an equivalent PHP data structure
###################################################################################
function & XML_unserialize(&$xml){
	$xml_parser = &new XML();
	$data = &$xml_parser->parse($xml);
	$xml_parser->destruct();
	return $data;
}
###################################################################################
# XML_serialize: serializes any PHP data structure into XML
# Takes one parameter: the data to serialize. Must be an array.
###################################################################################
function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
	if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
	while(list($key, $value) = each($data))
		if(!strpos($key, ' attr')) #if it's not an attribute
			#we don't treat attributes by themselves, so for an empty element
			# that has attributes you still need to set the element to NULL

			if(is_array($value) and array_key_exists(0, $value)){
				XML_serialize($value, $level, $key);
			}else{
				$tag = $prior_key ? $prior_key : $key;
				echo str_repeat("\t", $level),'<',$tag;
				if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
					while(list($attr_name, $attr_value) = each($data["$key attr"]))
						echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
					reset($data["$key attr"]);
				}

				if(is_null($value)) echo " />\n";
				elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
				else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
			}
	reset($data);
	if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
}
###################################################################################
# XML class: utility class to be used with PHP's XML handling functions
###################################################################################
class XML{
	var $parser;   #a reference to the XML parser
	var $document; #the entire XML structure built up so far
	var $parent;   #a pointer to the current parent - the parent will be an array
	var $stack;    #a stack of the most recent parent at each nesting level
	var $last_opened_tag; #keeps track of the last tag opened.

	function XML(){
 		$this->parser = &xml_parser_create();
		xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, 0);
		xml_set_object(&$this->parser, &$this);
		xml_set_element_handler(&$this->parser, 'open','close');
		xml_set_character_data_handler(&$this->parser, 'data');
	}
	function destruct(){ xml_parser_free(&$this->parser); }
	function & parse(&$data){
		$this->document = array();
		$this->stack    = array();
		$this->parent   = &$this->document;
		return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
	}
	function open(&$parser, $tag, $attributes){
		$this->data = ''; #stores temporary cdata
		$this->last_opened_tag = $tag;
		if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
			if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
				#this is the third or later instance of $tag we've come across
				$key = count_numeric_items($this->parent[$tag]);
			}else{
				#this is the second instance of $tag that we've seen. shift around
				if(array_key_exists("$tag attr",$this->parent)){
					$arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
					unset($this->parent["$tag attr"]);
				}else{
					$arr = array(&$this->parent[$tag]);
				}
				$this->parent[$tag] = &$arr;
				$key = 1;
			}
			$this->parent = &$this->parent[$tag];
		}else{
			$key = $tag;
		}
		if($attributes) $this->parent["$key attr"] = $attributes;
		$this->parent[$key] = NULL; #it turns out you can take a reference to NULL :)
		$this->parent       = &$this->parent[$key];
		$this->stack[]      = &$this->parent;
	}
	function data(&$parser, $data){
		if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
			$this->data .= $data;
	}
	function close(&$parser, $tag){
		if($this->last_opened_tag == $tag){
			if($this->data) $this->parent = $this->data;
			$this->last_opened_tag = NULL;
		}
		array_pop($this->stack);
		if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
	}
}
function count_numeric_items(&$array){
	return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Deprecated: Call-time pass-by-reference

Post by Christopher »

getgraphics wrote:I am trying to display weather on my site and I am getting many error messages such as:

Deprecated: Call-time pass-by-reference has been deprecated in xmllib.php on line 63

How can I fix this? There are 3 files below that are being used 1) weather.php 2) config.php and 3) xmllib.php
First of all, those are just warnings -- not errors -- so the code may still function correctly.

This is PHP4 code which passed objects in a different way that PHP5 does. You used to have to pass a reference to an object to be able to modify it in a function/method. In PHP5 objects are passed as a handle so this is not longer necessary.

To fix this code, you need to go through and find the objects. Then find where they are passed and remove the & before the variable passed and the parameter in the function/method.
(#10850)
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Deprecated: Call-time pass-by-reference

Post by cpetercarter »

This is a complex area, which I will try to summarise in a few sentences, at the risk that fellow board contributors will jump in and tell me I have got it wrong!

If you want to pass a variable to a php function, there are two possible ways of doing so. php can make a copy of the variable and give that to the function; or it can give the function a "reference" to the variable (ie it can let the function work on the original of the variable). The latter can be problematic as it allows functions to change the value of the variable for the rest of the programme, and in a large programme it can be difficult to track these changes.

In php 4, the default behaviour was that variables were passed by copying; but you could force pass-by-reference by placing an ampersand before the variable name (eg &$variable, instead of $variable). If you look through your script you will see quite a lot of variable names preceeded by & indicating pass by reference.

In php 5 "call-time pass-by-reference" has been deprecated. It still "works", but you get these annoying error messages. You can as a short term measure switch off error reporting to suppress the messages.

However, the underlying problem is that the code you have is starting to show its age. It is written for php 4, when it was common practice to pass objects around by reference, to stop php from making unnecessary copies of possibly complex objects. php 5 handles references to objects in a completely different way from php 4, and I suspect that in most cases the ampersand in front of the variable names can simply be removed and the script will run perfectly in php 5. But only the author can give you proper advice on this point, and as he has helpfully left the address of his website in the script I suggest you get in touch with him to ask. In fact, I see that he refers to the problem on his website, and says that he will in time produce a php 5 friendly version of his script. As php 5 has been around for some time now, he may have got the problem solved already.
getgraphics
Forum Newbie
Posts: 2
Joined: Mon Jun 28, 2010 11:12 pm

Re: Deprecated: Call-time pass-by-reference

Post by getgraphics »

Can you please give me one example from these files on how to "go through and find the objects. Then find where they are passed and remove the & before the variable passed and the parameter in the function/method."
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Deprecated: Call-time pass-by-reference

Post by Jade »

Find anything that has new in front of a function and remove the &.

This:

Code: Select all

$xml_parser = &new XML();
Should be:

Code: Select all

$xml_parser = new XML();
lagkdok
Forum Newbie
Posts: 1
Joined: Sun Jun 17, 2012 1:43 pm

Re: Deprecated: Call-time pass-by-reference

Post by lagkdok »

can i have the weather.css of this script plz

thanks
Post Reply