1

I recently updated my server php version from 5.6 to 7.3. When I attempt to load a page I'm seeing the following parse error:

Parse error: syntax error, unexpected 'new' (T_NEW) in /usr/share/pear/MDB2/Driver/mysqli.php on line 940

The file above is automatically installed via pear, here is the section prompting the error:

938 
939         $class_name = 'MDB2_Statement_'.$this->phptype;
940         $obj =& new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $        offset);
941         $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
942         return $obj;
943     }
944 

I've updated all pear packages using pear upgrade-all, restarted httpd and continue to get the same error above.

a coder
  • 7,530
  • 20
  • 84
  • 131
  • Do share the relevant code snippet with us... – jibsteroos Jan 22 '21 at 16:25
  • added snipped from PEAR's provided mysqli.php file – a coder Jan 22 '21 at 16:55
  • change `=&` to `=` – jibsteroos Jan 22 '21 at 16:57
  • Try and get rid of the `&` in `$obj =& new ...`. Objects shouldn't, and don't need to, be created by reference. – Markus AO Jan 22 '21 at 16:57
  • did a find/replace on all =&, that resolved the problem. If you'll make this an answer I'll approve and upvote you. Would appreciate an upvote here since I've updated the question text as requested. – a coder Jan 22 '21 at 17:05
  • For reference, this older thread explores through the `=& new` issue: [Assigning the return value of new by reference is deprecated](https://stackoverflow.com/questions/1086539/assigning-the-return-value-of-new-by-reference-is-deprecated) – Markus AO Jan 22 '21 at 18:33
  • I hope the question remains up since I wouldn't have known to look for the referenced question verbatim. Thanks for the answer. – a coder Jan 22 '21 at 20:06

1 Answers1

1

Your problem is with the =&, which should be simply =, in $obj =& new .... Objects can't, shouldn't, and don't need to, be created by reference. Such an expression was deprecated in PHP 5 and made invalid in PHP 7 (see eval response on different PHP versions).

It's a mystery to me why the PEAR package should have this, it must be a vestige from forever ago. It makes no more sense than $x =& []; (which would also result in an error). When an object is assigned to a variable, the variable becomes a pointer to the object. Therefore:

$a = new stdClass();
var_dump($a);
// object(stdClass)#1 (0) {}

$b = $a;
var_dump($b);
// object(stdClass)#1 (0) {}

var_dump($a === $b);
// bool(true)

That is: even without assigning $b =& $a, both variables point to the same object by default (ie. to object(stdClass)#1). Clean those up and drop a note to the PEAR package maintainers. FYI, the MDB2_Driver_mysqli package was last updated on 2012-10-23, so fetching the latest updates won't help much. Look for an up-to-date replacement. (Core PHP has had mysqli built in since PHP 5 and also has PDO if you need code portability between different RDBMs).

Markus AO
  • 4,771
  • 2
  • 18
  • 29
  • It looks like your package wasn't updated after all; the latest (beta) release (from 2012) updates the package to be PHP 5.3 compatible. Looking at the [source code](https://pear.php.net/package/MDB2_Driver_mysqli/docs/latest/__filesource/fsource_MDB2__MDB2_Driver_mysqli-1.5.0b4MDB2Drivermysqli.php.html), the "offending" line would be 1173, and there is no more `&=` there. Regardless, this package hasn't been updated for 9 years presumably because it has become largely obsolete with superseding developments in PHP core. – Markus AO Jan 22 '21 at 18:41
  • This legacy app was written about 12 years ago, so yeah - lots of code to rewrite. – a coder Jan 22 '21 at 20:04
  • ahh so basically I cannot use PEAR with php7.3... sad! – Jamie Hutber Apr 13 '22 at 11:28
  • Well you can use PEAR but not the MDB2 package that is basically abandoned. Really there's a lot of stuff in PEAR extensions that are poorly maintained. I personally don't trust anything PEAR to stay up to speed. Any dependency your application has on a given package there, be prepared to either be locked down to an old PHP version or fork and maintain a version for your needs. I haven't ever found a good reason to couple PEAR stuff into what I develop. – Markus AO Apr 13 '22 at 14:57