Jump to content

what to do about =& idiom?


ballhogjoni

Recommended Posts

I am getting an error saying Deprecated: Assigning the return value of new by reference is deprecated in /Users/xxx/dev/git/xxxx/cake/libs/inflector.php on line 131

 

I understand that this is because =& has been deprecated in php 5.3+, the question i have is what do I do about it? are the objects passed as references still or do I handle them some other way? My server is running 5.2.5 and my dev machine is running 5.3.2. Does this error act like a fatal error and break out of the script or does it act more like a notice?

 

Sorry for all the questions, just been out of php since php 4, don't know all the changes.

 

Thanks

Link to comment
Share on other sites

yes i sorted out the ini file.

 

here is the function:

function &getInstance() {

static $instance = array();

 

if (!$instance) {

$instance[0] =& new Inflector();

if (file_exists(CONFIGS.'inflections.php')) {

include(CONFIGS.'inflections.php');

$instance[0]->__pluralRules = $pluralRules;

$instance[0]->__uninflectedPlural = $uninflectedPlural;

$instance[0]->__irregularPlural = $irregularPlural;

$instance[0]->__singularRules = $singularRules;

$instance[0]->__uninflectedSingular = $uninflectedPlural;

$instance[0]->__irregularSingular = array_flip($irregularPlural);

}

}

return $instance[0];

}

 

These deprecated messages are coming from cakePHP's framework...so I guess they haven't updated yet.

Link to comment
Share on other sites

Mmm, that's new one on me ;p The manual states this:-

Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. Assignment by reference is also supported, using the $var = &$othervar; syntax. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere. To learn more about references, please read References explained. As of PHP 5, objects are assigned by reference unless explicitly told otherwise with the new clone  keyword.

 

Taken from this page so I guess that =& means to copy (PLEASE IF I AM WRONG ANYONE TELL ME!!!!) I actually thought it was similar to $$ (variable variable) which I try not to use so much.  Hope that helps a little, I haven't seen this sort of code in the wild before.. Then again, I do bespoke.

 

Then again, it's way past my bedtime, and I may have looked up the wrong type of operator, just sort through the options on the left hand menu until something looks familiar.

 

Rw

Link to comment
Share on other sites

It's not really a =& operator. It's a normal assignment operator (=) and a reference operator (&). I find it's confusing, and somewhat misleading, to write it like that, and I prefer to have it written this way:

 

$var = &$somethingelse;

 

In the code you posted, that usage was seen commonly in PHP 4 to circumvent issues caused by flaws in the poor OOP support. It is no longer necessary because as of PHP 5 all objects are, by default, passed by reference.

Link to comment
Share on other sites

^^^

Lol, nobody is blaming anyone, this is called learning.. No one is to blame when your learning..

 

@AlexWD: So at least I found the right page in the manual! It's actually pretty confusing as the manual has it like this and then there is the warning about using the reference operator within function & classes - but in essence the manual says that =& is correct, so I guess as it is a matter of your coding preferences.

 

Well, they say you learn something new every day!

 

Rw

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.