new operator and reference error

喜欢而已 提交于 2019-12-24 07:49:28

问题


 $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.


回答1:


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.




回答2:


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

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



回答3:


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;


来源:https://stackoverflow.com/questions/9732057/reference-from-a-newly-created-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!