Call-time pass-by-reference has been deprecated

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thierry

    Call-time pass-by-reference has been deprecated

    Hello all,

    The following code line :

    array_push($thi s->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.cla ss.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($t ag_name)
    {
    $this->tag_name = $tag_name;
    }

    function &get_attribute_ keys()
    {
    return array_keys($thi s->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($thi s->content, &$elt);
    }

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

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

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

    function export_end(&$re sult)
    {
    $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_k eys($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_k eys($save_conte nt) as $key)
    {
    $elt = &$save_conte nt[$key];
    if($id != "all" && $elt->get_id() != $id )
    {
    $this->add($elt);
    }
    }

    }
    }

    ?>

  • Alan Little

    #2
    Re: Call-time pass-by-reference has been deprecated

    Carved in mystic runes upon the very living rock, the last words of
    Thierry of comp.lang.php make plain:
    [color=blue]
    > Hello all,
    >
    > The following code line :
    >
    > array_push($thi s->content, &$elt);[/color]

    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

    Comment

    • Thierry

      #3
      Re: Call-time pass-by-reference has been deprecated

      [color=blue][color=green]
      >>The following code line :
      >>
      >>array_push($t his->content, &$elt);[/color]
      >
      >
      > 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?[/color]

      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($thi s->content, $elt), the final tag_name of
      my_tag2 is still "toto 2"...




      Comment

      • Justin Koivisto

        #4
        Re: Call-time pass-by-reference has been deprecated

        Thierry wrote:[color=blue]
        >[color=green][color=darkred]
        >>> The following code line :
        >>>
        >>> array_push($thi s->content, &$elt);[/color]
        >>
        >> 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?[/color]
        >
        > 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($thi s->content, $elt), the final tag_name of
        > my_tag2 is still "toto 2"...[/color]

        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,&$valu e_array){
        if(!is_array($t arget)){
        echo 'ERROR: Target is not an array in function array_push_ref. ';
        return FALSE;
        }
        if(is_array($va lue_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 - spam@koivi.com
        PHP POSTERS: Please use comp.lang.php for PHP related questions,
        alt.php* groups are not recommended.

        Comment

        Working...