PHP 中的引用变量可以用作长变量名的别名吗? [英] Can a reference variable in PHP be used to as an alias for long variable names?

查看:32
本文介绍了PHP 中的引用变量可以用作长变量名的别名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些人来说,这可能是一个愚蠢的问题,但我很好奇 PHP 会如何应对诸如:

This may be a silly question to some, but I was curious how would PHP react to something such as:

$blog_title = &$author->get_blog()->get_title();

我正在协助某人升级到内部 php 框架.有一些严重的方法链正在发生,在某些地方反复发生.我想让代码更具可读性,而没有不必要的变量副本.我无法彻底检查代码,所以我必须使用我所拥有的.

I'm assisting someone with some upgrades to an in house php framework. There is some serious method chaining going on, repeatedly in some places. I'd like to make the code more readable without unnecessary copies of variables. I'm unable to overhaul the code, so I have to work with what I've got.

我知道这可能不是一个很好的做法,但潜在的缺点是什么?

I know it's probably not a great practice, but what are the potential downsides?

推荐答案

只有当函数实际返回引用时才有意义(我的猜测是它可能不会),这意味着你要么希望返回保持不变完整脚本的静态,或者你正在破坏东西.您可以定义自己的短函数或回调,它们将为您进行链接,作为下一个最好的事情.

It would only make sense when the function actually returns a reference (my educated guess is it probably doesn't), which would mean you either hope the return stays static for the complete script, or you're breaking things. You could define your own short function or callback which will do the chaining for you as the next best thing.

无论如何,如果函数的返回值只需要计算一次,则将其保留在变量中,但是当函数的返回值不是一个时,不要指望引用有什么魔力.

By all means, keep a return of a function in a variable if it only needs to be evaluated once, but don't expect some magic from a reference when the return of a function is not one.

如果函数确实返回了一个引用,则代码可能在没有意义的地方使用了引用,您应该尽快修复它:P.

If the function does return a reference, the code is probably using references where they don't make sense, and you should fix that asap :P.

这篇关于PHP 中的引用变量可以用作长变量名的别名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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