严格标准:仅变量应通过引用 PHP 5.4 分配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11777908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strict Standards: Only variables should be assigned by reference PHP 5.4
提问by BBKing
I upgraded my PHP version to 5.4 (XAMPP1.7.3 to 1.8.0). Now I see Strict Standardserror, for myDBconnection:
我将 PHP 版本升级到 5.4(XAMPP1.7.3 到 1.8.0)。现在我看到严格的标准错误,对于myDBconnection:
Strict Standards: Only variables should be assigned by reference in C:\xampp\htdocs\alous\include\dbconn.php on line 4
严格标准:在 C:\xampp\htdocs\alous\include\dbconn.php 第 4 行中,只应通过引用分配变量
dbconn.php:
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.
注意:我不需要使用这种方法禁用 php.ini 中的严格标准error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT!我想修复我的 PHP 代码。
回答by Ozair Kafray
You should remove the &(ampersand) symbol, so that line 4 will look like this:
您应该删除&(与号)符号,以便第 4 行如下所示:
$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
这是因为 ADONewConnection 已经通过引用返回了一个对象。根据文档,从 PHP 5.3.0 开始,通过引用将引用的结果分配给对象会产生 E_DEPRECATED 消息。
回答by VettelS
It's because you're trying to assign an object by reference. Remove the ampersand and your script should work as intended.
这是因为您试图通过引用分配对象。删除&符号,您的脚本应该按预期工作。

