调用时传递引用不会生成E_DEPRECATED错误

时间:2011-10-19 15:58:52

标签: php

我遇到了E_DEPRECATED消息的一致性问题。

我有以下代码片段可以在命令行上运行来演示。

php -d error_reporting=-1 -d display_errors=1 -r 'function foo(&$bar) {return $bar->title . ".";} $bar = new stdClass(); $bar->title = "foobar"; print foo(&$bar) . PHP_EOL . PHP_VERSION . PHP_EOL;'

可读版本:

// error reporting set to E_ALL via command-line
function foo(&$bar) {
  return $bar->title . ".";
} 

$bar = new stdClass(); 
$bar->title = "foobar"; 

print foo(&$bar) . PHP_EOL . PHP_VERSION . PHP_EOL;
//        ^ this should raise E_DEPRECATED

我正在尝试使用此代码段PHP Deprecated: Call-time pass-by-reference has been deprecated触发foo(&$bar)消息,但由于某种原因,我的本地安装不会引发该消息。

我已将它传递给运行各种版本的php,不同操作系统等的其他几个环境,并获得了不同的结果:

预期:

mike@server:~$ php -d error_reporting=-1 -d display_errors=1 -r 'function foo(&$bar) {return $bar->title . ".";} $bar = new stdClass(); $bar->title = "foobar"; print foo(&$bar) . PHP_EOL . PHP_VERSION . PHP_EOL;'
PHP Deprecated:  Call-time pass-by-reference has been deprecated in Command line code on line 1
foobar.
5.3.5-1ubuntu7.2

在我的环境中:

mike@local:~$ php -d error_reporting=-1 -d display_errors=1 -r 'function foo(&$bar) {return $bar->title . ".";} $bar = new stdClass(); $bar->title = "foobar"; print foo(&$bar) . PHP_EOL . PHP_VERSION . PHP_EOL;'
foobar.
5.3.5-1ubuntu7.2

导致这种情况的原因是什么?

更新

我执行了以下操作来验证我的环境中是否仍然抛出了已弃用的错误:

mike@local:~$ php -d error_reporting=-1 -d display_errors=1 -r 'eregi("test", "test");'

Deprecated: Function eregi() is deprecated in Command line code on line 1

2 个答案:

答案 0 :(得分:0)

有可能,你的allow_call_time_pass_reference设置是否在php.ini中启用了?

; This directive allows you to enable and disable warnings which PHP will issue
; if you pass a value by reference at function call time. Passing values by
; reference at function call time is a deprecated feature which will be removed
; from PHP at some point in the near future. The acceptable method for passing a
; value by reference to a function is by declaring the reference in the functions
; definition, not at call time. This directive does not disable this feature, it
; only determines whether PHP will warn you about it or not. These warnings
; should enabled in development environments only.
; Default Value: On (Suppress warnings)
; Development Value: Off (Issue warnings)
; Production Value: Off (Issue warnings)
; http://www.php.net/manual/en/ini.core.php#ini.allow-call-time-pass-reference
allow_call_time_pass_reference = Off

特别注意:

; Default Value: On (Suppress warnings)

答案 1 :(得分:-1)

通过引用传递给函数已经被弃用了很长一段时间(我相信自从引入PHP 5以来?)并且这就是为什么你会收到警告的原因。简单地调用foo($bar)将通过引用传递参数,因为您已在函数定义中声明了这一点。

你获得不同结果的事实意味着php.ini文件中有一个不同或错误报告数量已关闭(我相信它会在版本之间发生变化)。