Diffrence between &new Object and new Object...

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Diffrence between &new Object and new Object...

    I must confess that I don't see the diffrence between using:

    $MyObject = new Object();
    and
    $MyObject = &new Object();

    Both expressions seem to produce new object and assign it to
    $MyObject... Is there any reason to use "&new" instead of "new"?
    kajaman (a.k.a. Hubert Łępicki ;)) Guest

  2. Similar Questions and Discussions

    1. Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object
      I've created user controls that contain listboxes that are dynamically populated from the database. In the html view of the user control...
    2. webservice.htc and custom object array. only first object is deserialized in the result.value
      Hello I've been using webservice.htc for 6 months or so with great results. recently i came into the following problem that I am not sure how to...
    3. Custom Control Problem :: Object reference not set to an instance of an object
      Hi All! I have the following Custom Control file... '########### WebUserControl1.ascx ############# <%@ Control Language="vb"...
    4. [WebMethod] System.NullReferenceException: Object reference not set to an instance of an object.
      Um, this isn't going to work, generally. Web services, as any web app (especially on Windows server 2003) are heavily sandboxed. The method you...
    5. Cannot serialize object of type System.Object[,]. Multidimensional arrays are not supported
      Hi, I get this on server when trying to retun a 2 dim array. I apprecaite that they are not supported as per...
  3. #2

    Default Re: Diffrence between &new Object and new Object...


    There is no &new. The syntax is actually...

    $MyObject =& new Object();

    where =& is an assignment operator that creates a reference.

    To see the difference, try this...

    // example class
    class MyClass
    {
    var $m_strValue;
    function MyClass()
    {
    // initialise
    $this->m_strValue = -1;
    }
    function SetValue( $p_strNewValue )
    {
    $this->m_strValue = $p_strNewValue;
    }
    function GetValue()
    {
    return $this->m_strValue;
    }
    }

    $objMyObject1 = new MyClass();
    print $objMyObject1->GetValue() . "\n";

    // -1

    $objMyObject1->SetValue(2);
    print $objMyObject1->GetValue() . "\n";

    // 2


    $objMyObject2 =& new MyClass();
    print $objMyObject1->GetValue() . "\n";

    // 2 - would have been -1 if I hadn't created a reference!
    See [url]http://www.php.net/manual/en/language.references.php[/url]

    ---
    Steve

    Steve Guest

  4. #3

    Default Re: Diffrence between &new Object and new Object...

    Steve wrote:
    > There is no &new. The syntax is actually...
    >
    > $MyObject =& new Object();
    >
    > where =& is an assignment operator that creates a reference.
    >
    > To see the difference, try this...
    >
    > // example class
    > class MyClass
    > {
    > var $m_strValue;
    > function MyClass()
    > {
    > // initialise
    > $this->m_strValue = -1;
    > }
    > function SetValue( $p_strNewValue )
    > {
    > $this->m_strValue = $p_strNewValue;
    > }
    > function GetValue()
    > {
    > return $this->m_strValue;
    > }
    > }
    >
    > $objMyObject1 = new MyClass();
    > print $objMyObject1->GetValue() . "\n";
    >
    > // -1
    >
    > $objMyObject1->SetValue(2);
    > print $objMyObject1->GetValue() . "\n";
    >
    > // 2
    >
    >
    > $objMyObject2 =& new MyClass();
    > print $objMyObject1->GetValue() . "\n";
    [cut]
    about the last line: shouldn't it be:
    print $objMyObject2->GetValue(). "\n";
    ?

    Ok, I will make some experiments, and I'll see how it works.
    kajaman (a.k.a. Hubert Łępicki ;)) Guest

  5. #4

    Default Re: Diffrence between &new Object and new Object...

    Ok, but I can't see any difference! It doesn't matter if I change = into
    =& , printed values are the same!

    I use PHP 5 - maybe this is the reason?
    kajaman (a.k.a. Hubert Łępicki ;)) Guest

  6. #5

    Default Re: Diffrence between &new Object and new Object...

    > about the last line: shouldn't it be:
    > print $objMyObject2->GetValue(). "\n";
    Oh dear. Yup, it should be. And, yup, when you correct that code
    there's no difference between the two objects. Oh dear.

    ---
    Steve

    Steve Guest

  7. #6

    Default Re: Diffrence between &new Object and new Object...


    The difference is:

    $MyObject = new Object();

    does:
    1. creates Object
    2. makes a copy of it (not using the class standard constructor)
    3. assigns the copy to $MyObject variable

    $MyObject = & new Object();

    does:
    1. creates Object
    2. assigns the object to $MyObject variable

    Why? Cause "=" is a copying operator. When you use "= &" then you
    get reference copied.


    Example below shows the difference (you get "oryginal" objects in $instances array,
    and copy in $instance variable). On PHP v 4.3.7 I get this result:

    $instance_counter: 0

    $instance = new MyClass();
    $instance_counter: 1
    $instance->name: default name
    $instances[count($instances)-1]->name: default name
    $instance->name = "some name";
    $instance->name: some name
    $instances[count($instances)-1]->name: default name

    $instance = & new MyClass();
    $instance_counter: 2
    $instance->name: default name
    $instances[count($instances)-1]->name: default name
    $instance->name = "other name";
    $instance->name: other name
    $instances[count($instances)-1]->name: other name


    Hilarion

    <?php
    $instance_counter = 0;
    $instances = array();

    class MyClass {
    var $name;
    function MyClass() {
    global $instances, $instance_counter;
    $this->name = 'default name';
    $instances[] = &$this;
    $instance_counter++;
    }
    }

    echo '$instance_counter: ' . $instance_counter . "\r\n\r\n";

    echo '$instance = new MyClass();'."\r\n";
    $instance = new MyClass();

    echo '$instance_counter: ' . $instance_counter . "\r\n";

    echo '$instance->name: ' . $instance->name . "\r\n";
    echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n";

    echo '$instance->name = "some name";'."\r\n";
    $instance->name = "some name";

    echo '$instance->name: ' . $instance->name . "\r\n";
    echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n\r\n";

    echo '$instance = & new MyClass();'."\r\n";
    $instance = & new MyClass();

    echo '$instance_counter: ' . $instance_counter . "\r\n";

    echo '$instance->name: ' . $instance->name . "\r\n";
    echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n";

    echo '$instance->name = "other name";'."\r\n";
    $instance->name = "other name";

    echo '$instance->name: ' . $instance->name . "\r\n";
    echo '$instances[count($instances)-1]->name: ' . $instances[count($instances)-1]->name . "\r\n\r\n";
    ?>


    Hilarion Guest

  8. #7

    Default Re: Diffrence between &new Object and new Object...

    My php version is Version 5.3.0,and the result is :


    $instance_counter: 0



    $instance = new MyClass();

    $instance_counter: 1

    $instance->name: default name

    $instances[count($instances)-1]->name: default name

    $instance->name = "some name";

    $instance->name: some name

    $instances[count($instances)-1]->name: some name



    $instance = & new MyClass();

    $instance_counter: 2

    $instance->name: default name

    $instances[count($instances)-1]->name: default name

    $instance->name = "other name";

    $instance->name: other name

    $instances[count($instances)-1]->name: other name
    mmLiu Guest

  9. #8

    Default Re: Diffrence between &new Object and new Object...

    HELLO
    how are you doing today? i hope all is well.
    My name is Miss Sonia emmedy i saw your profile today in[www.justskins.com]and became interested in you,l will also like to know you the more,pls contact me in my private email so l will give you my picture for you to know whom l am. Here is my email address (soniaemmedy3@hotmail.com) I believe we can move from here! I am waiting for your mail to my email address above. Miss Sonia (Remember the distance or color does not matter but love matters a lot in life)
    tamara30 is offline Junior Member
    Join Date
    Mar 2011
    Location
    liberan
    Posts
    4

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139