You are here: Re: =& when creating new object « PHP Programming Language « IT news, forums, messages
Re: =& when creating new object

Posted by Jerry Stuckle on 09/25/07 00:14

Shelly wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@comcast.com...
>> Shelly wrote:
>>> "gosha bine" <stereofrog@gmail.com> wrote in message
>>> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>>> Shelly wrote:
>>>>> "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.
>>>>>
>>>>> Shelly
>>>> php references have nothing to do with C-alike pointers.
>>>>
>>>> Please read the chapter called "references are not pointers" in the
>>>> manual.
>>>>
>>> Yeah, yeah. We still are talking about address-of and not value-of. So
>>> there is no explicit pointer variable as there is in C. So what?
>>>
>>> Shelly
>> No, it's not address-of, either in PHP or C++. It's more "alias-of".
>
> ...and by alias of you mean another way of referencing the same place in
> memory that contains the value as the thing it is aliasing. --- aka
> address-of. Change the value contained in the "reference" and the value of
> the "referenced" changes. Change the value located at the "address of" and
> the value of the other variable changes. Six of one, a half dozen of the
> other. *ptr->foo=junk does the same thing as saying foo=junk. Also
> fee=&foo and *fee=junk does the same thing. (It has been about seven years
> six I did any C coding (so my memory of exact syntax may be a little off),
> but it all comes down to the Bard -- "a rose by any other name....".
>

Well, of course everything in the computer has an address. But you
should not consider it an address in PHP (PHP doesn't HAVE addresses).
It might be referencing a hash value in a table, for instance, and that
table may have no fixed address.

Additionally, in C++, it is NOT an address. You cannot change what item
is being referenced in C++, for instance. It is truly an alias.

The difference being in C++ you do have addresses - they are used in
pointer variables. And you can change the address (contents of the
pointer) to point at another location in memory.

*ptr->foo = junk is NOT the same as foo=junk. *ptr->foo is pointing to
a class or structure member (class/structure type unknown) named "foo".
foo=junk is referencing a non-class/structure variable. You could say
*ptr=junk, but only if ptr contains the address of foo.

Additionally, in C++ you must have an exact match between types on each
side - no conversions (i.e. int->double) allowed. References allow
conversions (casting is not a conversion!).

I could continue - but there are significant differences between
pointers in references in C++.

> You say pot-tay-to and I say po-tah-to.
>
> Shelly
>
>

Not at all. It is quite important to keep those straight, as I tell my
C++ students.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация