只能通过引用传递变量 [英] Only variables can be passed by reference

查看:144
本文介绍了只能通过引用传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码给出(有和没有自定义错误处理程序):

我有一个很好的想法,使用一个自定义的错误处理程序, strong>致命错误:只能通过引用传递变量

  function foo(){
$ b = array_pop(阵列( A, b, C));
return $ b;
}
print_r(foo());

以下代码(仅限于仅使用自定义错误处理程序):(2048)只能通过引用传递变量

  function foo(){
$ a = explode('/','a / b / c');
$ c = array_pop(array_slice($ a,-2,1));
return $ c;
}
print_r(foo());

第二个担心我,因为我有很多紧凑代码。所以,我或者说愚蠢的想法是使用自定义的错误处理程序(改进我的日志记录模块)或扩展我的所有代码。



任何人有更好的想法?另外,WTF?



更新



感谢我的答案了解了PHP如何处理错误。 E_ALL不包括E_STRICT(php 5)的混淆并不是很酷。



除此之外,创建自己的自定义错误处理程序默认情况下可以启用E_STRICT,那些问题开始。



故事的道德是使用自己的错误处理程序来捕获它们,并使用错误常量(E_STRICT,E_USER_WARNING,E_USER_ERROR等)来执行您的过滤。



对于具有变量引用和某些功能的内存损坏问题,可以说什么?双重非冷却我会(这不意味着你应该)在我的错误处理程序中忽略E_STRICT,生命继续下去。

解决方案

array_pop )尝试更改作为参数传递的值。现在在第二个例子中,这是array_slice()的返回值。在引擎术语中,这是一个临时值,这样的值不能被引用传递。您需要的是一个临时变量:

  function foo(){
$ a = explode('/' 'A / b / C');
$ b = array_slice($ a,-2,1);
$ c = array_pop($ b);
return $ c;
}
print_r(foo());

然后对$ b的引用可以传递给array_pop()。有关引用的更多详细信息,请参阅 http://php.net/references


I had the bright idea of using a custom error handler which led me down a rabbit hole.

Following code gives (with and without custom error handler): Fatal error: Only variables can be passed by reference

function foo(){
    $b=array_pop(array("a","b","c"));
    return $b;
}
print_r(foo());

Following code gives (only with a custom error handler): (2048) Only variables should be passed by reference

function foo(){
    $a=explode( '/' , 'a/b/c');
    $c=array_pop(array_slice($a,-2,1));
    return $c;
}
print_r(foo());

The second one worries me since I have a lot of 'compact' code. So, I either ditch the bright idea of using a custom error handler (to improve my logging module) or expand all my code.

Anyone with better ideas? Also, WTF?

UPDATE:

Thanks to the answers I've learnt something about how php does error handling. The confusion of E_ALL not including E_STRICT (php 5) is not cool.

On top of all this, creating your own custom error handler enables E_STRICT by default and thats where problems start.

The moral of the story is to use your own error handler to catch them ALL and use the error constants (E_STRICT, E_USER_WARNING, E_USER_ERROR, etc.) to do your filtering.

As for the 'memory corruption issue' with variable references and certain functions, what can I say? Doubly uncool. I'll (which doesn't mean you should) ignore E_STRICT in my error handler and life goes on.

解决方案

array_pop() tries to change that value which is passed as parameter. Now in your second example this is the return value from array_slice(). In engine terms this is a "temporary value" and such a value can't be passed by references. what you need is a temporary variable:

function foo(){
    $a=explode( '/' , 'a/b/c');
    $b=array_slice($a,-2,1);
    $c=array_pop($b);
    return $c;
}
print_r(foo());

Then a reference to $b can be passed to array_pop(). See http://php.net/references for more details on references.

这篇关于只能通过引用传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆