Viewed   76 times

I've just got an error.

When I try to assign an object like this:

$obj_md = new MDB2();

The error I get is "Assigning the return value of new by reference is deprecated". Actually I've been looking for a solution but the only one I've seen is just turn down the politicy of php.ini (error_reporting). I've tried it too, but it didn't work.

It's so confusing..I hope you could help me. Thanks in advance.

 Answers

4

In PHP5 this idiom is deprecated

$obj_md =& new MDB2();

You sure you've not missed an ampersand in your sample code? That would generate the warning you state, but it is not required and can be removed.

To see why this idiom was used in PHP4, see this manual page (note that PHP4 is long dead and this link is to an archived version of the relevant page)

Tuesday, October 4, 2022
1

Your feeling is right. It will generally work fine, but there are edge cases where it doesn't.

Using =& has these differences with =:

  • =& will try to make the right side yield a reference; = won't -- even if the right side is able to yield a reference, like a function which returns by reference.
  • =& will break the old reference set and put the left and the right side both in a new one, while = will change the value of all the elements in the same reference set as the left side to the value of the right side.

The first difference and half of the second is irrelevant in this case. After the assignment, there will be only one variable with the value of the new object*, and single-element reference sets make no sense. However, the fact that =& breaks the previous reference set is significant:

<?php

$g = 4;
$v =& $g;
$v = new stdclass();
var_dump($g); // object(stdClass)#1 (0) { }

$g = 4;
$v =& $g;
$v =& new stdclass();
var_dump($g); // int(4)

* Unless maybe the constructor leaks a reference, but even if it leaks, $this inside the constructor may be a different variable, even if it points to the same object. So I doubt one could observe behavior difference due to this.

Monday, September 12, 2022
3

I use following wrapper class in my php5.2 apps: http://pastebin.ca/2051944. Untill php5.3 was released - it saves much my time

Thursday, November 3, 2022
 
2

It is not allowed to bind the temporary to a non-const reference, but if you make your reference const you will extend the lifetime of the temporary to the reference, see this Danny Kalev post about it.

In short:

const A& mySecondObject = myFunction();
Tuesday, August 2, 2022
3
// [...]
/**
 * Return the Request object
 *
 * @return Zend_Controller_Request_Abstract
 */
public function getRequest()
{
    return $this->_request;
}
// [...]

works perfectly with Eclipse PDT. Which plugin do you use?

Friday, August 19, 2022
 
rofls
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :