views:

73

answers:

2

Was wondering if it is possible to make a variable point to another variable instead of having it have a value of its own. What I'm trying to do is to have a class instance like:

$users = new User_Model();

and then have

$user

simply point to

$users

instead of making a new class instance. Is this possible? Think I saw something about it in the php manual, but cant find it..

Would

$users = new User_Model();
$user = $users;

simply do it or is somehow possible (as I've asked above) to make $user act simply as a "wormhole" to $users?

Thanks

+1  A: 

$user = &$users;

http://php.net/manual/en/language.references.php

sjobe
+5  A: 

By default in PHP 5 objects are copied by reference. So when you do

$users = new User_Model();
$user = $users;

Both $user and $users point to the same object.

However primitive types are still passed by value

$va = 1;
$vb = $va;
$va = 2;
echo $vb; //1

So you need to take the reference of the primitive value;

$va = 1;
$vb = &$va;
$va = 2;
echo $vb; //2
Yacoby