Page 1 of 1

Object Creation Question

Posted: Sat Aug 20, 2005 4:27 am
by Ree
When will I want to use

Code: Select all

$object = new Object();
and when

Code: Select all

$object = &new Object();
?

In the examples/articles related to OOP I find the second case more often. Would like someone to explain me this. I do undertand what a reference is, but I don't understand why would I want to initially create a reference to a new class instance. So far, I've always used

Code: Select all

$object = new Object();

Posted: Sat Aug 20, 2005 8:47 am
by feyd
in some instances (versions) of php, the object is copied to the variable, not automatically referenced. The ampersand explicitly tells PHP to store and use the reference. At least, that's what I've read in various locations.. I do it because I think it's good practice, and if php was going to use the reference anyways, it doesn't really hurt anything :)

Posted: Sat Aug 20, 2005 2:40 pm
by Ree
Well I don't get it...

If I want an object, I use $object = new Object(). That means that I just got a new instance of the class Object in the variable $object, in short I just got an object. If I want to create a reference to that object, I could then do $ref = &$object, so in this case I would have the object itself ($object) and the reference to it ($ref).

Now where does $object = &new Object() come to play? Why do I need it at all? I even don't understand exactly what that means. If $object is a reference, then... what is it reference to? How can I access the thing which $object is the reference to? And overall, what's wrong with $object = new Object()?

Tricky stuff. :?

Posted: Sat Aug 20, 2005 5:55 pm
by feyd
here's an illustration:

Code: Select all

<?php

class foo
{
  function foo()
  {
    $this->bar();
  }

  function bar()
  {
    $this->{'a'.mt_rand()} = time();
  }
}

$foo = $bar =& new foo();
var_export($bar === $foo);
echo "\n\n\$bar\n";
$bar->bar();
print_r($bar);
echo "\n\n\$foo\n";
print_r($foo);
echo "\n";
var_export($bar === $foo);

?>
php 5.0.4

Code: Select all

true

$bar
foo Object
(
    [a1582282270] => 1124578478
    [a2134674650] => 1124578478
)


$foo
foo Object
(
    [a1582282270] => 1124578478
    [a2134674650] => 1124578478
)

true
php 4.3.8

Code: Select all

true

$bar
foo Object
(
    [a1438208314] => 1124578417
    [a780000713] => 1124578417
)


$foo
foo Object
(
    [a1438208314] => 1124578417
)

false

Posted: Sat Aug 20, 2005 9:18 pm
by raghavan20
object = &new Object();
The use of &, the reference operator might look quite strange to guys who have come from C++ and other OO langs as it appeared to me first.
It happens like this,
when you say new Object();, a memory block is allocated with a starting memory location and usually the object, object here is given the physical name for the memory location or atleast it points to that location.
As you may infer from feyd, the object gets created and a copy of that object is made and allocated when object - new Object();
when you use &new Object(), I think no copy of the object is made instead the first created object is used.