[clug-progsig] PHP operator
Shawn
sgrover at open2space.com
Wed Mar 8 00:05:25 PST 2006
On Tuesday 07 March 2006 23:50, Nick Wiltshire wrote:
> It appears you are right. I found this paragraph:
>
> Note that the assignment copies the original variable to the new one
> (assignment by value), so changes to one will not affect the other. This
> may also have relevance if you need to copy something like a large array
> inside a tight loop. Since PHP 4, assignment by reference has been
> supported, using the $var = &$othervar; syntax, but this is not possible in
> PHP 3. 'Assignment by reference' means that both variables end up pointing
> at the same data, and nothing is copied anywhere.
>
> So does that mean these are equivalent?
>
> function MyFunction(&$x) { /*do something*/ }
> $x = new MyClass();
> MyFunction($x);
> --------------------------
> function MyFunction($x) { /*do something*/ }
> $x =& new MyClass();
> MyFunction($x);
>
> The difference being when the reference is created?
>
> Nick
In the first function, you are declaring the parameter to be a "by reference"
parameter - i.e. the variable $x points to a memory location rather than
containting a value.
In the second function you are saying that the variable $x points to the
memory location for the instance of MyClass() being created.
If we stopped there, then they are different. But the final line in both
examples is saying to pass $x to MyFunction(). The parameter within the
function would be a pointer to a memory location - in both cases. So you
could think of them as the same, but I think there's a slight gotcha there.
In the second case, I believe you would end up with a copy of the pointer
getting passed. So you would have two variables - $x outside the function
and $x inside the function - that both pointed to the same place in memory,
but the inside version would be treated as a copy of a reference - if that
makes sense. Unless you are doing something really twisted, I don't think
the difference would matter any.
er, now that I take another look (it IS getting late, please bear with me..
LOL).... I think the first example would not do what you expect. The logic
would go like this:
- create a new instance of the MyClass() object
- the value returned by creating the instance (which is typically a memory
addres for complex variables), is assigned to the variable $x
- Then you call MyFunction which will take the address of a variable as a
parameter.
- So, you would end up passing the address of $x - not necessarily the object
instance. It depends on how PHP stores references. If the address of the
object is stored as the value of $x, then you'd have a problem. But if $x is
simply treated like an alias to a memory location, then things would work
properly.
I'm sure I'm making this tougher than it really is, but it has been a long day
and I'm not really thinking toooooo clearly right now. But this would be
REAL easy to test/prove - write the code, and dump the address of the $x
variable at every step.
Anyways, hope this doesn't confuse you toooooo much...
Shawn
More information about the clug-progsig
mailing list