3

I'm working with some old PHP code that has a lot of the following:

$someVar =& new SomeClass();

Did the new operator ever return a value, um, not by reference? (That feels strange to type. I feel like I'm losing my mind.)

3 Answers 3

5

It was one of those sort of optimization techniques taught in a lot of older books on OOP in PHP 4.

Basically, the initial object created in memory is one the application can't access unless you return the instance by reference. Otherwise you get a copy of the object - the only catch is that the original exists without a symbol. Kinda dumb.

But ya, object creating and passing and references in PHP 4 is a monumental mess.

Sign up to request clarification or add additional context in comments.

Comments

4

Thats PHP4 code. From the documentation: (now removed)

"new" does not return a reference by default, instead it returns a copy.

[1]: http://no.php.net/manual/en/language.oop.newref.php

Comments

1

See also my answer here which includes a simple code sample to illustrate the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.