PHP warning: Call-time pass-by-reference has been deprecated -


i getting warning: call-time pass-by-reference has been deprecated following lines of code:

function xml() {     $this->parser = &xml_parser_create();     xml_parser_set_option(&$this->parser, xml_option_case_folding, false);     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; } 

what cause , how fix it?

remove & &$this everywhere, not needed. in fact, think can remove & everywhere in code - not needed @ all.

long explanation

php allows pass variables in 2 ways: "by value" , "by reference". first way ("by value"), can't modify them, other second way ("by reference") can:

     function not_modified($x) { $x = $x+1; }      function modified(&$x) { $x = $x+1; } 

note & sign. if call modified on variable, modified, if call not_modified, after returns value of argument same.

older version of php allowed simulate behavior of modified not_modified doing this: not_modified(&$x). "call-time pass reference". deprecated , should never used.

additionally, in ancient php versions (read: php 4 , before), if modify objects, should pass reference, use of &$this. neither necessary nor recommended anymore, object modified when passed function, i.e. works:

   function obj_modified($obj) { $obj->x = $obj->x+1; } 

this modify $obj->x though formally passed "by value", passed object handle (like in java, etc.) , not copy of object, in php 4.

this means, unless you're doing weird, never need pass object (and $this reference, call-time or otherwise). in particular, code doesn't need it.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -