Your link merely recommends using & to prevent a copy being made. I recommend not using & since when you do do move to php5 you will need to go through and delete all the &'s.
"merely" ? You're either downplaying the importance of instantiating by reference or you do not fully understand the consequences that come of instantiating by copy.
First thing's first however...
Three things
1: I did not claim for the link I posted to be a comprehensive guide to the reference operator. It's merely a piece in the puzzle.
2: I feel what I posted answered the thread author's question. That was enough for me.
He asked, the link I posted answered the question.
What does the '&' do when placed in front of new?
e.g.
$myClass = new Game;
and
$myClass = &new Game;
3: More importantly. I disagree with your recommendation. ... with a passion!
... ok maybe not with a passion, but with lukewarm sentiment. 😃
What you are suggesting will have the following effects.
Consider this code excerpt:
$myObject = new Phone();
There are three things happening here.
The "new Phone()" statement creates an instance of a Phone object.
You then copy the Phone object to the $myObject variable.
The first instance of the phone object is then deleted from memory.
Now consider this code exceprt:
$myObject =& new Phone();
The "new Phone()" statement creates an instance of a Phone object.
The $myObject variable points/refers to the original instance of the Phone object.
There's a big difference between the two.
To add to this, consider the following classes:
class Web_Ring {
var $_website;
function load_site(&$website) {
$this->_website = &$website;
}
function display() {
$this->_website->output();
}
}
class Website
{
var $_data;
function Website(&$portal) {
$portal->load_site($this);
}
function set_data($data) {
$this->_data = $data;
}
function output() {
print $this->_data;
}
}
This implementation will display the correct information.
$web_ring = &new Web_Ring();
$website = &new Website($web_ring); // using the reference operator.
$website->set_data('This is a virtual website which is part of a web ring');
$web_ring->display(); // displays 'This is a virtual website which is part of a web ring'
This will not display anything.
$web_ring = &new Web_Ring();
$website = new Website($web_ring); // no reference operator
$website->set_data('This is a virtual website which is part of a web ring');
$web_ring->display(); // displays nothing
With regards to PHP5 migration, a quick search and replace would fix your problem.
Search for "=& new" replace with "= new".
I could give you a whole migration strategy, but I'll stop here. 😉
Hope this clears some things up for you.
Regards,