SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    May 2003
    Location
    nyc
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what does "=&" mean in "$something =& new Something;"?

    hi all

    the subject says it all. what does "=&" mean when creating an instance of Something? Thanks a lot

    james

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why did you post this in two seperate forums? I've already answered this.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it's not creating a new instance. That's just the point! By specifying the & you are requesting PHP to point at the existing variable in memory and not create a copy. Example:
    PHP Code:
    <?php
    $member
    ['name'] = "Torrent";
    $name =& $member['name']; // sets name to Torrent
    $member['name'] = "madeonmoon";

    // will print madeonmoon, not Torrent as $name points to $member['name']
    print $name
    ?>

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh!

  5. #5
    SitePoint Addict
    Join Date
    May 2003
    Location
    nyc
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the answer. this makes sense. as for the double-posting, dumb of me. was just too impatient -- 5 mins to get the answer was simply too long

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    double-posting = evil.

    You will now get hit repeatedly with pies for 5 minutes.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    SitePoint Addict
    Join Date
    May 2003
    Location
    nyc
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok

  8. #8
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try and give a bit of an explanation as to exactly what is going on when you create an instance of a class.

    Take this example class:
    PHP Code:
    class Test
    {
      var 
    $str ;
      function 
    Test()
      {
        
    $this->str "I am a class" ;
        echo 
    "hello" ;
      }
      function 
    getStr()
      {
        return 
    $this->str ;
      }

    Now I can create an instance of this class thus:
    PHP Code:
    new Test 
    Now, when I do this, the class object is created and 'hello' is printed to the screen. However, I haven't created any way of referencing the object for future use (say I wanted to get the value of $str using the getStr() method).

    Now, if I was to do the following:
    PHP Code:
    $test = new Test 
    As before, it creates a new instance of the Test object, and creates a copy of the object in a variable called $test (so now we have 2 copies of the Test object, one that is pretty much useless to us, and one that we can actually get at.

    So, if I do this:
    PHP Code:
    $testRef =& new Test 
    I have, as before, created an instance of the Test object, but in this case I create a variable $testRef that doesn't contain the class itself, but rather a reference (or pointer) to that object.

    Using either of the last 2 methods, you can of course now access the member functions of the class.
    PHP Code:
    echo $test->getStr() ;

    // or

    echo $testRef->getStr() ;

    //both of which will print out 'I am a class' 
    Now, there are cases when creating copies can result in your programs behaving in unexpected ways due to chances to member data happening in some copies but not others. But this is something you probably would encounter without creating very large and complicated systems.

    In PHP5, all objects are instanciated by reference by default.

    I hope this clears things up a bit in your mind.

    Matt. [img]images/smilies/smile.gif[/img]
    Last edited by Mincer; May 16, 2003 at 04:01.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •