Hi.

What does the '&' do when placed in front of new?

e.g.

$myClass = new Game;
and
$myClass = &new Game;

Thanks.
K.

    The easy answer is to upgrade to php5 and & goes away for objects.

    A slightly more complex answer is that even in php4, the two statements are effectivily identical for 99.99% of the classes you might run across so it's not worth worrying about.

    If you really want to know when the statements could yield different results then try the manual:
    http://www.php.net/manual/en/language.oop.newref.php

    Bottom line is that $myClass = new Game actually creates a copy of the object just constructed while $myClass =& new Game does not.

    I'd suggest not using the & since php5 no longer needs it.

      Ah, references, of course - and yes they will not affect what I am doing!

      Thanks!
      K.

        Kold wrote:

        What does the '&' do when placed in front of new?

        e.g.

        $myClass = new Game;
        and
        $myClass = &new Game;

        There's a big difference between the two methods of instantiation.

        Here's a link that covers your questions regarding this matter.
        http://www.sitepoint.com/article/object-oriented-php/7

        Regards,

          rvdavid wrote:

          There's a big difference between the two methods of instantiation.

          Here's a link that covers your questions regarding this matter.
          http://www.sitepoint.com/article/object-oriented-php/7

          Regards,

          Actually, as I explained in my first post, there is seldom any difference between the two methods as far as functionality goes. Your code runs the same regardless except in a few specific cases.

          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.

          Your link barely touches on the constructor issues and does not even mention the fact that sometimes you have to use & to get the expected behaviour.

            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,

              Write a Reply...