Php By Reference

前端 未结 5 950
夕颜
夕颜 2020-12-17 06:03

Can someone please explain what the \"&\" does in the following:

class TEST {

}

$abc =& new TEST();

I know it is by reference. Bu

5条回答
  •  渐次进展
    2020-12-17 06:47

    Firstly, you don't really need to use it if you are using PHP 5, in PHP 5 all objects are passed by reference by default.

    Secondly, when you assign an object to a variable name, either by creation, passing in a parameter, or setting a variable value, you are either doing so by reference or value.

    Passing by reference means you pass the actual memory reference for the object, so say you passed an object as a parameter to a function, any changes that function makes to that variable will be reflected in the parent method as well, you are actually changing the state of that object in memory.

    The alternative, to pass by value means you pass a copy of that object, not the memory reference, so any changes you make, will not be reflected in the original.

提交回复
热议问题