Re: =& when creating new object
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Tue, 25 Sep 2007 06:43:35 -0400
Steve wrote:
"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message news:VLWdnbBpwNDwzmXbnZ2dnUVZ_gmdnZ2d@xxxxxxxxxxxxxxShelly wrote:"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@xxxxxxxxxxxxxxWell, 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.Shelly wrote:...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...."."gosha bine" <stereofrog@xxxxxxxxx> wrote in message news:46f823bf$0$31121$6e1ede2f@xxxxxxxxxxxxxxxxxNo, it's not address-of, either in PHP or C++. It's more "alias-of".Shelly wrote: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?"Steve" <no.one@xxxxxxxxxxx> wrote in message news:r6VJi.36$Nr6.32@xxxxxxxxxxxxxxxphp references have nothing to do with C-alike pointers."Joe" <joe@xxxxxxxxx> wrote in message news:1190663828.772978.15800@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxThat 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.I am just starting to use Object Oriented PHP coding, and I am seeingwell, 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'.
quite often the following (this example taken from a wiki):
$wakka =& new Wakka($wakkaConfig);
What exactly is the =&, and why is it different from = ?
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.
Shelly
Please read the chapter called "references are not pointers" in the manual.
Shelly
well, when dealing with a topic that is hard to explain as it is to a newbie who could care less about what php does behind the scenes, a good common ground language would be pointers since they are in most other modern languages.
Well, a newbie has no idea what a "pointer" is. Or what it does behind the scenes. Many people have never programmed in C/C++, for instance - and the majority of other languages (Java, Fortran, COBOL, Perl, PASCAL, Basic... the list goes on) have no concept of pointers.
Pointers are a VERY DIFFICULT subject for people who have never been exposed to them, no matter what their experience. In my C and C++ classes, it is the most difficult concept for programmers experienced in non-pointer languages to grasp. In fact, most beginning programmers have an easier time than experienced ones.
Much better to use non-technical terms, such as "alias".
i don't think the op is familiar with it anyway based on the way the question was posed. that's why my first response demonstrated the behavior rather than a technically correct response - one that would have been lost any way, on the op.
And unnecessarily complicated the description.
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.
actually, if you want to get technical, each variable points to an address in memory that address may contain either data for the datatype specified, or it may contain a pointer to another address...that may contain either data for the datatype specified, or it may contain a pointer to another...
Sure. But there is a distinct difference between a pointer and a reference. COBOL and FORTRAN both have variables. They both have something which acts like references. But they don't have pointers.
PHP has references. It does not have pointers. What goes on under the covers is immaterial.
but, who gives a *** really. we're trying to get a point across that is not simply made either way. is this helping the op, or edifying someone else?
The person who doesn't understand because you unnecessarily complicated the explanation with technical gobbly-gook?
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.
and this is C++? btw, did you not see me change the variables in my example from being references to independent 'memory locations'. don't get technical here, we're talking about behavior. i did exactly what you said php doesn't have the ability to do...if i understand you correctly here.
I was discussing C++ implementation with Shelly, not your code.
*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.
arguing his example gets you no where since his point is that you can get the same *behavior* from php in most cases that you can in C++ regarding references. if you want to go off on semantics, fine. however, remember that php is a different animal than C++. that means, in most cases, you're going to be comparing apples to oranges.
Yes, I know they are different. I am using C++ as an example because it has both pointers and references. It provides a valid comparison between the two. You can't make that comparison in PHP because it doesn't have pointers.
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!).
this would be an apple and an orange example, case in point! do you expect *any scripting* language to *require* strong datatyping? that's one of the strengths of scripting languages.
Not at all. It's a difference between pointers and references.
further, are you fully prepared to discuss in a meaningful way to the op, how memory works, what conversions are and what casting is...not just how php does any of them, but C++ also?
I sure can. Can you?
I could continue - but there are significant differences between pointers in references in C++.
that's great, however, we're talking about php. essentially, the behavior is such, in this example, that making any distinction is just semantics and very useless to the op.
We are talking about the differences between pointers and references, using a language which has both. PHP doesn't have pointers.
You say pot-tay-to and I say po-tah-to.Not at all. It is quite important to keep those straight, as I tell my C++ students.
Shelly
good for them that they have you, i suppose. i'm sure, being the seasoned teacher that you are, you wouldn't dare cover such a topic on day one, would you?
;^)
No, we don't normally get into it until the afternoon of day 2.
But then we do an entire C++ course in 5 days. It's what corporations want. And I've been doing it for almost 17 years now.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- Follow-Ups:
- Re: =& when creating new object
- From: Steve
- Re: =& when creating new object
- References:
- =& when creating new object
- From: Joe
- Re: =& when creating new object
- From: Steve
- Re: =& when creating new object
- From: Shelly
- Re: =& when creating new object
- From: gosha bine
- Re: =& when creating new object
- From: Shelly
- Re: =& when creating new object
- From: Jerry Stuckle
- Re: =& when creating new object
- From: Shelly
- Re: =& when creating new object
- From: Jerry Stuckle
- Re: =& when creating new object
- From: Steve
- =& when creating new object
- Prev by Date: Re: Creating a watchlist
- Next by Date: Re: includes and paths and organization
- Previous by thread: Re: =& when creating new object
- Next by thread: Re: =& when creating new object
- Index(es):