Kingston Town
Kingston Town

Reputation: 63

new operator and reference error

 $class_name = 'MDB2_Statement_'.$this->phptype;
        $statement = null;
        $obj =& new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);

The above code is old, I would like to change it into something like

$class_name='MDB2_Statement_'.$this->phptype;
...
$obj_=new class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
$obj=&$obj_;

But this doesn't result in correct behaviors. Could anyone offer me a fix ?

[UPDATE] if i leave the old code as it is, I run into some "deprecated warnings" in every view page that is loaded

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\htdocs...\APPLI\php\library\PEAR\MDB2.php on line 391

for example.

I open that file and would like to edit it in some way (I don't know) so as for the Deprecated to go away. The only current solution is to reinstall the XAMPP that contains appropriate PHP version to match the one the old was written in. I am at a loss as to figure out any way to deal with these warnings without any reinstallation, it is certain that I don't want to see those Deprecated on top of every page at all. Thank you.

Upvotes: 0

Views: 395

Answers (3)

Niclas Larsson
Niclas Larsson

Reputation: 1317

Why do you even use the reference operator with objects? A object is placed in the memory and all variables bound to it will change its memory.

In other words

$a1 = new stdClass;
$a2 = $a1;

would have the same affect as:

$a1 = 1000;
$a2 = &$a1;

Upvotes: 0

deceze
deceze

Reputation: 522135

The only thing PHP is complaining about is this:

$obj =& new $class_name...
      ^

You do not need and should not use assignment by reference anymore, since objects are always references in PHP 5. Just get rid of the &, and that's it.

Upvotes: 3

Onkar Janwa
Onkar Janwa

Reputation: 3950

You can create reference of an object in php 5.3.10 using this way.

$firstObj = new something();
$referenceObj = &$firstObj;

Upvotes: 0

Related Questions