如何更换'调用时间按引用传递关于 PHP
考虑到 PHP 已经弃用了一些函数和功能,我想重构我的代码以适应 php 5.3。
现在,我必须消除所有“调用时间传递引用”。所以,我有三个问题:
1 - 如果我替换:
$myclass->myfunc(&$myvar);
by
$myvar = $myclass->myfunc($myvar);
会起作用吗?
2 - 如果我有类似的情况该怎么办?
$myclass->myfunc(&$myVar, &$ourvar);
3 -
$x = &new myclass();
谢谢您的宝贵时间,我们将非常感谢您的帮助
Considering that PHP has already deprecated a few functions and functionallitys, I would like to refactory my codes to fit it on php 5.3.
now, I have to eliminate all the 'Call-time pass-by-reference' . So, I have three questions:
1 - If I replace:
$myclass->myfunc(&$myvar);
by
$myvar = $myclass->myfunc($myvar);
will work ?
2 - what do I do if I have something like that?
$myclass->myfunc(&$myVar, &$ourvar);
3 - How about
$x = &new myclass();
Thanks for your time, any help will be very appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您熟悉什么是通话时间传递尊重吗?正常的按引用传递是通过在其声明中的参数前面添加
&
来声明某些参数按引用传递的函数来完成的。调用时按引用传递意味着该函数被声明为按值获取这些参数,并且您在事后将其行为更改为按引用传递。通过引用传递调用时间实际上并不是必要的。每个函数都应该有一个特定的目的,并且为了正确实现其目的,它应该始终通过引用获取参数,或者始终通过值获取参数。让一个函数做一些它没有设计目的的事情是不好的。
回答有关
$myclass->myfunc(&$myvar);
和$myclass->myfunc(&$myVar, &$ourvar);
的问题> 我想说的是,如果您需要通过引用传递该函数,那么应该将其声明为始终通过引用传递。即然后要使用它,您只需调用它而无需
&
$x = &new myclass();
是完全无关的。你没有传递任何东西。它仍然是有效的语法。First, are you familiar with what call-time pass by reverence is? Normal pass by reference is accomplished by the function declaring that certain arguments are pass by reference by prepending a
&
to the parameter in its declaration.Call-time pass by reference means that the function is declared to take those arguments by value, and you are changing its behavior to pass-by-reference after the fact. Call-time pass by reference shouldn't really ever be necessary. Every function should have a specific purpose, and to correctly accomplish its purpose, it should either always take an argument by reference, or always take it by value. It is bad to make a function do something it was not designed to do.
Responding to your questions about
$myclass->myfunc(&$myvar);
and$myclass->myfunc(&$myVar, &$ourvar);
I would say that, if you need to pass by reference to that function, then it should be declared as always pass by reference. i.e.Then to use it you just call it without the
&
$x = &new myclass();
is completely irrelevant. You are not passing anything. It is still valid syntax.