471,354 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Call-time pass-by-reference has been deprecated

Hello all,

The following code line :

array_push($this->content, &$elt);

produces the following error :

Warning: Call-time pass-by-reference has been deprecated - argument
passed by value; If you would like to pass it by reference, modify the
declaration of array_push(). If you would like to enable call-time
pass-by-reference, you can set allow_call_time_pass_reference to true in
your INI file. However, future versions may not support this any longer.
in /home/sites/site77/web/lib/xml/General_tag.class.php on line 56

What is the propest way to fix it ?

Thx, Thierry.

PS 1 : I can not modify the "online" php.ini file. I'm using PHP v4.1.2
online, and v4.2.0 on local for my tests.

PS 2 : Here is the complete source code.

<?php

/**
* General_tag
*/

class General_tag
{
var $id = "";
var $tag_name = "noname";
var $attributes = array();
var $content = array();

function get_id()
{
return $this->id;
}

function set_id($id)
{
$this->id = $id;
}

function get_tag_name()
{
return $this->tag_name;
}

function set_tag_name($tag_name)
{
$this->tag_name = $tag_name;
}

function &get_attribute_keys()
{
return array_keys($this->attributes);
}

function get_attribute($key)
{
return $this->attributes[$key];
}

function put_attribute($key, $value)
{
$this->attributes[$key] = $value;
}

function is_empty()
{
return count($this->content) == 0;
}

function add(&$elt)
{
array_push($this->content, &$elt);
}

function &get_content()
{
return $this->content;
}

function export(&$result)
{
$this->export_start($result);
$this->export_content($result);
$this->export_end($result);
}

function export_start(&$result)
{
$result .= "<" . $this->get_tag_name();
while (list ($key, $value) = each ($this->attributes))
{
$result .= " " . $key . "=\"" . $value . "\"";
}
$result .= ">";
}

function export_end(&$result)
{
$result .= "</" . $this->get_tag_name() . ">";
}

function export_content(&$result)
{
foreach($this->content as $elt)
{
$elt->export($result);
}
}

function &find_by_id($id)
{
if($this->get_id() == $id)
{
return $this;
}
else
{
$content = &$this->get_content();
foreach(array_keys($content) as $key)
{
$elt = &$content[$key];
$result = &$elt->find_by_id($id);
if($result != null)
{
return $result;
}
}

return null;
}
}

function remove_content_by_id($id)
{
$save_content = $this->get_content();
$this->content = array();
foreach(array_keys($save_content) as $key)
{
$elt = &$save_content[$key];
if($id != "all" && $elt->get_id() != $id )
{
$this->add($elt);
}
}

}
}

?>

Jul 17 '05 #1
3 24972
Carved in mystic runes upon the very living rock, the last words of
Thierry of comp.lang.php make plain:
Hello all,

The following code line :

array_push($this->content, &$elt);


Why would you want to do this? $elt is simply pushed onto the array; it
isn't modified in any way. Why are you trying to pass it by reference?

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #2
The following code line :

array_push($this->content, &$elt);

Why would you want to do this? $elt is simply pushed onto the array; it
isn't modified in any way. Why are you trying to pass it by reference?


Because I use to do that :

$my_tag = &new General_tag();
$my_tag->set_tag_name("toto");

$my_tag2 = &new General_tag();
$my_tag2->set-tag_name("toto 2");

// Here I use it
$my_tag->add($my_tag2);

// Here it becomes important to have the reference.
// In general, done in an other function.
$my_tag2->set_tag_name("toto 3");

with only array_push($this->content, $elt), the final tag_name of
my_tag2 is still "toto 2"...


Jul 17 '05 #3
Thierry wrote:
The following code line :

array_push($this->content, &$elt);


Why would you want to do this? $elt is simply pushed onto the array;
it isn't modified in any way. Why are you trying to pass it by reference?


Because I use to do that :

$my_tag = &new General_tag();
$my_tag->set_tag_name("toto");

$my_tag2 = &new General_tag();
$my_tag2->set-tag_name("toto 2");

// Here I use it
$my_tag->add($my_tag2);

// Here it becomes important to have the reference.
// In general, done in an other function.
$my_tag2->set_tag_name("toto 3");

with only array_push($this->content, $elt), the final tag_name of
my_tag2 is still "toto 2"...


To get around the call-time reference passing issue, simply create a
function that will do what you want. For instance, something like this
may work for your purposes:

function array_push_ref(&$target,&$value_array){
if(!is_array($target)){
echo 'ERROR: Target is not an array in function array_push_ref.';
return FALSE;
}
if(is_array($value_array)){
foreach($value_array as $val)
$target[]=$val
}
}else{
echo 'WARNING: Passed value is not an array, treating as single
value.';
$target[]=$value_array;
}
return TRUE;
}
--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fabian Müller | last post: by
8 posts views Thread by trying_to_learn | last post: by
7 posts views Thread by rahul8143 | last post: by
1 post views Thread by news.onet.pl | last post: by
3 posts views Thread by harborboy76 | last post: by
46 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by cberthu | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.