What is the meaning of the ampersand '&' before the keyword 'new'?

Why would you do this?

$a = &new <someclass>(); 

For example, the documentation for SimpleTest SimpleBrowser uses this syntax ( http://www.simpletest.org/en/browser_documentation.html ).

 $browser = &new SimpleBrowser(); 

Is it possible to use this? Is this a relic of PHP 4?

Edit:

I understand that an ampersand is returned by reference, but what is the return point of a new instance of an object by reference?

+6
source share
2 answers

In PHP5, objects are passed using opaque object descriptors . You can still make a reference to a variable holding such a descriptor and give it a different value; this is what the &new constructor does in PHP5. This does not seem particularly useful, although if you will not clone it explicitly, there is only one copy of a specific instance of the object, and you can reference its descriptors anytime after creating the instance, if you want. Thus, I assume that the code you found is a delay when &new was a necessary pattern.

+3
source

Since PHP5 new automatically returns links. Thus, using =& pointless in this context (and if I'm not mistaken, giving the E_STRICT message).

Pre-PHP5 using =& was to get a reference to an object. If you initialized an object into a variable and then assigned both variables working on the same object to this new variable, exactly the same as today in PHP5.

+2
source

Source: https://habr.com/ru/post/900038/


All Articles