|
Posted by Steve on 09/24/07 20:49
"Shelly" <sheldonlg.news@asap-consult.com> wrote in message
news:13fg87gm6uq9i9e@corp.supernews.com...
>
> "Steve" <no.one@example.com> wrote in message
> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>
>> "Joe" <joe@faceh.com> wrote in message
>> news:1190663828.772978.15800@22g2000hsm.googlegroups.com...
>>>I am just starting to use Object Oriented PHP coding, and I am seeing
>>> quite often the following (this example taken from a wiki):
>>>
>>> $wakka =& new Wakka($wakkaConfig);
>>>
>>> What exactly is the =&, and why is it different from = ?
>>
>> well, better syntax would have helped. it should read ' $something =
>> &$variable'. in this context, & means 'a reference to the memory location
>> where the value is stored'. without the &, it means 'a copy of the value
>> of the variable'.
>>
>> clear as mud? rather than explain memory, let me have you do this:
>>
>> $variable = 'hello';
>> $reference = &$variable;
>> $variable = 'world';
>> echo '<pre>' . print_r($reference, true) . '</pre>';
>> $reference = 'good-bye';
>> echo '<pre>' . print_r($variable, true) . '</pre>';
>>
>> unset($variable);
>> unset($reference);
>>
>> $variable = 'hello';
>> $reference = $variable;
>> $variable = 'world';
>> echo '<pre>' . print_r($reference, true) . '</pre>';
>> $reference = 'good-bye';
>> echo '<pre>' . print_r($variable, true) . '</pre>';
>>
>> the first example, BY REFERENCE, means that both $variable and $reference
>> point to the same memory location. changing one but using the other makes
>> no difference - using either will have the same effect.
>>
>> the second, BY VALUE, means that each variable points to two different
>> locations in memory. the only time they will be equal is when setting
>> them so. once you modify one of them, you have done so idependently of
>> the other - changing one has no effect on the other.
>>
>> hope that makes more sense.
>
> That was probably the single most unique new concept (pointers and
> address-of) I had conquer when (os so many years ago) I learned C, coming
> from a Fortran background as I did.
and i bet it opened up an entirely new world to your mind to set off
on...one that is constructed much more simply that without it, right? yeah,
that was a big deal for me too...going from BASIC to C. before BASIC, i
worked directly with memory registers in PLC's. i just thought i'd have to
give up that ability when i got into BASIC. then i saw the construct and an
example in a C programming book. BINGO, i had it all again! what's funny is
the book explained it poorly (too verbose), however the best example i found
yeeeaaaarrrrssss later was on php.net in their docs. a single four line
example that showed what was going on.
[Back to original message]
|