Page 1 of 1

Newbie: classes help...

Posted: Wed Apr 13, 2005 9:00 pm
by gen23
Hi,

Can anyone please explain what this statement means?

Code: Select all

class & execute (&$controller, &$request, &$user)
{
$renderer =& new renderer ($controller, $request, $user);
}
or even this one:

Code: Select all

class first_class
{
$obj1 = new first_class(); 
$obj2 = new first_class();
}
Thank you in advance...

Phenom | Please use

Code: Select all

Tags[/color]

Posted: Thu Apr 14, 2005 7:22 am
by John Cartwright
& is used to reference things, and your classes are creating new objects.

For example

$obj1 = new first_class();

You have now created an object which instantiates your class. Although it does not make sense to instantiate your class inside the class, because it must already be done to use the class. Unless of course it is static.

Your first example uses reference.

$var = 'blah';

$var2 &= $var now references to $var

Posted: Thu Apr 14, 2005 10:12 am
by pickle
Ya, using the & makes the operation a reference, rather than an equality. If you use =&, you are essentially making a new shortcut to the value. For example:

Code: Select all

$x = 2;
$y = $x;
$z =& $x;

$x++;

echo $y . '|' . $z;
... would echo '2|3'.

Code: Select all

$x = 2;
$z =& $x;

$z++;
echo $x;
... would echo '3' again.

Using the =& operator just allows another variable name to be pointed to the value of the first variable. Essentially, you'll have two pointers to the same data.