47

I upgraded my PHP version to 5.4 (XAMPP 1.7.3 to 1.8.0). Now I see Strict Standards error, for myDBconnection:

Strict Standards: Only variables should be assigned by reference in C:\xampp\htdocs\alous\include\dbconn.php on line 4

dbconn.php:

<?php
    defined('_VALID') or die('Restricted Access!');

    $conn = &ADONewConnection($config['db_type']); // <--- This Line 4

    if ( !$conn->Connect($config['db_host'],
                         $config['db_user'],
                         $config['db_pass'],
                         $config['db_name'])) {

        echo 'Could not connect to MySQL! Please check your database settings!';
        die();
    }
    $conn->execute("SET NAMES 'utf8'");
?>

Note: I don't need to disable Strict Standards in php.ini with this method error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT! I want to fix my PHP code.

Community
  • 1
  • 1
BBKing
  • 2,279
  • 8
  • 38
  • 44

2 Answers2

81

You should remove the & (ampersand) symbol, so that line 4 will look like this:

$conn = ADONewConnection($config['db_type']);

This is because ADONewConnection already returns an object by reference. As per documentation, assigning the result of a reference to object by reference results in an E_DEPRECATED message as of PHP 5.3.0

Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
  • 6
    He's not using `new` though. – sam Oct 22 '13 at 21:16
  • 14
    Yes, `&` should be removed in this instance, but not for the reason give (as @sam suggests). It's simply that the result of the assignment is not a _variable_, which results in an E_STRICT message, not an E_DEPRECATED warning, which is what happens with the `new` operator. In fact, using `=&` could actually be valid if the `ADONewConnection()` _function_ is set to [return a reference](http://www.php.net/manual/en/language.references.return.php). – MrWhite Apr 29 '14 at 14:17
  • Can you add the reason why it should be removed (by [editing your answer](https://stackoverflow.com/posts/11778004/edit) - not here in comments)? – Peter Mortensen Apr 03 '20 at 16:24
10

It's because you're trying to assign an object by reference. Remove the ampersand and your script should work as intended.

VettelS
  • 1,204
  • 1
  • 9
  • 17