1

Was wondering if it's possible to have a variable point to another variable in PHP? 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 again.

Would

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

simply do it?

Thanks

1
  • 1
    lol. Wormhole. I wish they were called that instead of references. Commented Apr 19, 2010 at 15:13

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

1

$user = &$users;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.