Home » Php » PHP 5 and "& new"

PHP 5 and "& new"

Posted by: admin July 12, 2020 Leave a comment


this statement:

$node_obj =& new someClass($somearg, $moreargs);

generate an error in the latest version of PHP right?

I have tried it on my machine (PHP >= 5.0.0 & <=5.2.17) and I get this error:

Deprecated: Assigning the return value of new by reference is deprecated in test.php on line 1108

How to&Answers:

Remove the &.

Objects are passed as reference by default in PHP 5, which is why using the & is deprecated in this context.


So, you’ve got this or similar error:

Parse error: syntax error, unexpected 'new' (T_NEW) in 
/usr/share/php/PEAR/Config.php on line 650

Proper fix would be to upgrade all PEAR libraries. But since (as time of writing) there’s none, here’s how to fix all these errors in PEAR when running PHP 7.

Before the fix.

find /usr/share/php -type f -name \*.php | xargs grep '&new' | wc -l

Returns 77 occurrences.

Now comes sed magic

find /usr/share/php -type f -name \*.php | xargs sed -i 's/&new/new/g'
find /usr/share/php -type f -name \*.php | xargs sed -i 's/& new/ new/g'

After the fix

Previous code returns zero occurrences.

You may also want to add this somewhere in PEAR.php:

function set_magic_quotes_runtime() {}