Object Creation Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Object Creation Question

Post 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();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 :)
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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. :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
Post Reply