Re: =& when creating new object




"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message
news:i6WdnSgveO9ee2XbnZ2dnUVZ_gadnZ2d@xxxxxxxxxxxxxx
Steve wrote:
"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message
news:VLWdnbBpwNDwzmXbnZ2dnUVZ_gmdnZ2d@xxxxxxxxxxxxxx
Shelly wrote:
"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message
news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@xxxxxxxxxxxxxx
Shelly wrote:
"gosha bine" <stereofrog@xxxxxxxxx> wrote in message
news:46f823bf$0$31121$6e1ede2f@xxxxxxxxxxxxxxxxx
Shelly wrote:
"Steve" <no.one@xxxxxxxxxxx> wrote in message
news:r6VJi.36$Nr6.32@xxxxxxxxxxxxxxx
"Joe" <joe@xxxxxxxxx> wrote in message
news:1190663828.772978.15800@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.

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".

very good point. however, i would hope that if a term (the only technical
one i gave) were unfamiliar to the op, he'd google it and find a full
definition with examples. that's just too much to cover in here. anyway, if
i use a non-technical term like 'alias', then i'm stuck explaining
everything to the op in here.

again, that's why i focused on the example rather than a full-blown
explanation.

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.

actually, the first description was as reduced as possible while still not
becoming inaccurate. it really was a setup for the example. he has something
to look at to figure out the description based on what he observes. you do
have a good point though...i could have said 'pointer', 'reference', and
then described 'alias' instead. that way he could google that if need be.

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.

again, let me reiterate:

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?

i explained it the best i could...and i gave an example. i think that is
sufficient. what i don't get is the fact that you make that statement, all
the while doing a techie-deepthrought about stuff that is far above the op's
paygrade and doesn't even address HIS question. you are addressing what you
though was OUR lack of knowledge in C++ and php. i don't see how this
benefitted the op, nor do i see you quickly spit-shining your kettle...my
pot had just come out of the washer and was fairly clean on this topic.

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.

i know, and i don't care about my code. what you said was:

"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."

i read that as a connected comparison. that was my attempt to show that the
behavior could be emulated.

*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.

which is my whole point! C++ is being compared to PHP *because* php doesn't
have pointers...yet, the behavior is so similar that it becomes a matter of
semantics in most cases. still however, such a comparison is still apples
(c++) to oranges (php).

if you want me to use 'alias' instead of 'pointer', i'd consider it...same
with 'reference'. if i don't, then don't go all psycho...give me the exact
laymens terminology you think i should use. either way, for the question at
hand, there are only semantic differences and getting this technical with
people who already understand C++ is not helping anyone. the only thing that
would, is what definition of & would most make jerry comfortable.


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?

prepared, yes...willing to, no. i think i may just be more pragmatic about
the whole matter. a post the size of a small novel is not generally a
welcomed thing.

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.

yet the *behavior* in this situation is identicle to pointers in C++. i'm
lost on any further argument you've got since the paygrade of the op
couldn't care less about the technicality of it all.

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.

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.

ROFLMFAO! and then they jump right into developing mission critical
applications, right?

i spent 3 semesters in the university learning C++. that was what the
UNIVERSITY wanted...they're more picky about their reputation, i suspect.

5 days. wow, i bow to your amazing talents jerry!


.