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 = ?
Re: =& when creating new object
am 24.09.2007 22:26:21 von Michael Fesser
..oO(Joe)
>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);
It was required in PHP 4 to make sure that $wakka will be assigned a
reference to the created object instead of just a copy of it, which
could lead to problems in some situations.
If you just start with OOP in PHP, then you should do this with PHP 5,
where objects are always passed as references by default and you don't
have to worry about this issue anymore (at least not for objects).
Micha
Re: =& when creating new object
am 24.09.2007 22:29:32 von Steve
"Joe" 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:
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.
Re: =& when creating new object
am 24.09.2007 22:31:43 von Steve
"Michael Fesser" wrote in message
news:d57gf3p144kqni8fvaan5kra2f2d9vntbj@4ax.com...
> .oO(Joe)
>
>>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);
>
> It was required in PHP 4 to make sure that $wakka will be assigned a
> reference to the created object instead of just a copy of it, which
> could lead to problems in some situations.
>
> If you just start with OOP in PHP, then you should do this with PHP 5,
> where objects are always passed as references by default and you don't
> have to worry about this issue anymore (at least not for objects).
you've implied that byVal and byRef are restricted only to php versions and
applied only to objects. i'm sure that's not the message you want to get
across here, right?
Re: =& when creating new object
am 24.09.2007 22:40:19 von Shelly
"Steve" wrote in message
news:r6VJi.36$Nr6.32@newsfe05.lga...
>
> "Joe" 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 '
';
>
> 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
Re: =& when creating new object
am 24.09.2007 22:45:00 von joe
Steve, thats a great explanation, thank you.
I have a question however. The WIKI example I'm looking at calls =&
new Wakka.... on something that has not been defined yet. It is making
a reference to something that doesnt exist? isnt it?
On Sep 24, 9:31 pm, "Steve" wrote:
> "Michael Fesser" wrote in message
>
> news:d57gf3p144kqni8fvaan5kra2f2d9vntbj@4ax.com...
>
> > .oO(Joe)
>
> >>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);
>
> > It was required in PHP 4 to make sure that $wakka will be assigned a
> > reference to the created object instead of just a copy of it, which
> > could lead to problems in some situations.
>
> > If you just start with OOP in PHP, then you should do this with PHP 5,
> > where objects are always passed as references by default and you don't
> > have to worry about this issue anymore (at least not for objects).
>
> you've implied that byVal and byRef are restricted only to php versions and
> applied only to objects. i'm sure that's not the message you want to get
> across here, right?
Re: =& when creating new object
am 24.09.2007 22:49:13 von Steve
"Shelly" wrote in message
news:13fg87gm6uq9i9e@corp.supernews.com...
>
> "Steve" wrote in message
> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>
>> "Joe" 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 '
';
>>
>> 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.
Re: =& when creating new object
am 24.09.2007 22:51:04 von gosha bine
Shelly wrote:
> "Steve" wrote in message
> news:r6VJi.36$Nr6.32@newsfe05.lga...
>> "Joe" 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 '
';
>>
>> 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.
>you've implied that byVal and byRef are restricted only to php versions and
>applied only to objects.
Have I? References are explained in detail in the manual and the OP was
explicitly talking about OOP. But OK, I got your point.
Micha
Re: =& when creating new object
am 24.09.2007 22:52:43 von Steve
"Joe" wrote in message
news:1190666700.795975.303100@r29g2000hsg.googlegroups.com.. .
> Steve, thats a great explanation, thank you.
>
> I have a question however. The WIKI example I'm looking at calls =&
> new Wakka.... on something that has not been defined yet. It is making
> a reference to something that doesnt exist? isnt it?
kind of, but no.
think of it as if you read that example from right to left. that is how most
variable assignments should be read by you when confusion sets in. anyway,
first the object is created by php. then php, through the & instruction,
returns the 'pointer' of where that object is in memory to the variable
getting the assignment.
make sense? right to left.
let me know.
Re: =& when creating new object
am 24.09.2007 22:53:59 von Shelly
"Joe" wrote in message
news:1190666700.795975.303100@r29g2000hsg.googlegroups.com.. .
> Steve, thats a great explanation, thank you.
>
> I have a question however. The WIKI example I'm looking at calls =&
> new Wakka.... on something that has not been defined yet. It is making
> a reference to something that doesnt exist? isnt it?
No. Wakka has been defined as a class in a file there on the system (and
has been included somewhere). So, the new command creates an instance of
that class and the & points to address of that new instance.
Shelly
Re: =& when creating new object
am 24.09.2007 22:56:33 von Steve
"Michael Fesser" wrote in message
news:1p8gf3hrsm2tlj12dopnpctdvm7ug3ndv3@4ax.com...
> .oO(Steve)
>
>>you've implied that byVal and byRef are restricted only to php versions
>>and
>>applied only to objects.
>
> Have I? References are explained in detail in the manual and the OP was
> explicitly talking about OOP. But OK, I got your point.
that was just the way i read it and didn't want the op to get confused. he
was using wiki afterall. ;^) i thought the question more about byVal/byRef
moreso than oop.
sall good, momma. ;^)
Re: =& when creating new object
am 24.09.2007 23:08:06 von Steve
"gosha bine" wrote in message
news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
> Shelly wrote:
>> "Steve" wrote in message
>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>> "Joe" 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 '
';
>>>
>>> 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.
'chapter' ? oh, you mean the quoted text of a usenet post? may i ask, who is
jani?
" For the 2nd time: references ARE NOT POINTERS! :-p
--Jani
"
hmmm, i see the strong case being made here! kind of like:
"for the nth time! toothfairies ARE REAL!"
gosha, you'd be wise at this point to draw the distinctions yourself (if not
capable, use a supported reference [no pun]). but, since references provide
identical behaviors to pointers, the only difference that i see is the
literal words themselves (one, 'pointer', the other, 'reference')...and
perhaps some symantics somewhere.
as it is, REFERENCES like POINTERS speak to POINTING TO AN ADDRESS or a COPY
OF A VALUE AT AN ADDRESS...which is EXACTLY WHAT A POINTER IS/DOES. not to
mention that AT LENGTH, php has discussed that the biggest change/hurdle
when moving from php < 5 to php >= 5 will be how objects are going to be
passed BY REFERENCE instead of byVal from here on out.
however, since you don't like me calling you an idiot when i see you being
one, you have killfiled my responses...like a child. so, you won't even feel
corrected. if you do read this post, and not being able to provide a valid
counter, you'll pretend not to see this post out of shear convenience...and
the side-effect of saving face.
Re: =& when creating new object
am 24.09.2007 23:22:32 von Michael Fesser
..oO(Steve)
>"gosha bine" wrote in message
>news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>
>> Please read the chapter called "references are not pointers" in the
>> manual.
>
>'chapter' ? oh, you mean the quoted text of a usenet post? may i ask, who is
>jani?
From the manual (not the UCNs):
| They are not like C pointers; instead, they are symbol table aliases.
| Note that in PHP, variable name and variable content are different, so
| the same content can have different names.
>gosha, you'd be wise at this point to draw the distinctions yourself (if not
>capable, use a supported reference [no pun]). but, since references provide
>identical behaviors to pointers
They don't. A pointer contains an address in memory, which could be a
variable or the entry point of a function. You can even point to a
pointer to a pointer - a thing that _cannot_ be done with references,
because a reference is just an alias name for something.
>the only difference that i see is the
>literal words themselves (one, 'pointer', the other, 'reference')...and
>perhaps some symantics somewhere.
Even in C/C++ pointers and references are two different concepts.
>as it is, REFERENCES like POINTERS speak to POINTING TO AN ADDRESS or a COPY
>OF A VALUE AT AN ADDRESS...which is EXACTLY WHAT A POINTER IS/DOES.
Correct, but that's not what a reference does.
Micha
Re: =& when creating new object
am 24.09.2007 23:24:55 von joe
Right to left makes sense. Thanks.
I'm still unsure of cases where I should use & and where I should not,
but I guess that will come from reading more examples.
On Sep 24, 10:08 pm, "Steve" wrote:
> "gosha bine" wrote in message
>
> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>
>
>
> > Shelly wrote:
> >> "Steve" wrote in message
> >>news:r6VJi.36$Nr6.32@newsfe05.lga...
> >>> "Joe" 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 '
';
>
> >>> 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.
>
> 'chapter' ? oh, you mean the quoted text of a usenet post? may i ask, who is
> jani?
>
> " For the 2nd time: references ARE NOT POINTERS! :-p
>
> --Jani
> "
>
> hmmm, i see the strong case being made here! kind of like:
>
> "for the nth time! toothfairies ARE REAL!"
>
> gosha, you'd be wise at this point to draw the distinctions yourself (if not
> capable, use a supported reference [no pun]). but, since references provide
> identical behaviors to pointers, the only difference that i see is the
> literal words themselves (one, 'pointer', the other, 'reference')...and
> perhaps some symantics somewhere.
>
> as it is, REFERENCES like POINTERS speak to POINTING TO AN ADDRESS or a COPY
> OF A VALUE AT AN ADDRESS...which is EXACTLY WHAT A POINTER IS/DOES. not to
> mention that AT LENGTH, php has discussed that the biggest change/hurdle
> when moving from php < 5 to php >= 5 will be how objects are going to be
> passed BY REFERENCE instead of byVal from here on out.
>
> however, since you don't like me calling you an idiot when i see you being
> one, you have killfiled my responses...like a child. so, you won't even feel
> corrected. if you do read this post, and not being able to provide a valid
> counter, you'll pretend not to see this post out of shear convenience...and
> the side-effect of saving face.
Re: =& when creating new object
am 24.09.2007 23:35:44 von Steve
"Michael Fesser" wrote in message
news:60agf3dds15g16okej94srele60sifocfu@4ax.com...
> .oO(Steve)
>
>>"gosha bine" wrote in message
>>news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>>
>>> Please read the chapter called "references are not pointers" in the
>>> manual.
>>
>>'chapter' ? oh, you mean the quoted text of a usenet post? may i ask, who
>>is
>>jani?
>
> From the manual (not the UCNs):
>
> | They are not like C pointers; instead, they are symbol table aliases.
> | Note that in PHP, variable name and variable content are different, so
> | the same content can have different names.
yes, however very nearly all semantics at this point.
>>gosha, you'd be wise at this point to draw the distinctions yourself (if
>>not
>>capable, use a supported reference [no pun]). but, since references
>>provide
>>identical behaviors to pointers
>
> They don't. A pointer contains an address in memory, which could be a
> variable or the entry point of a function. You can even point to a
> pointer to a pointer - a thing that _cannot_ be done with references,
> because a reference is just an alias name for something.
and THIS would be the distinction.
>>the only difference that i see is the
>>literal words themselves (one, 'pointer', the other, 'reference')...and
>>perhaps some symantics somewhere.
>
> Even in C/C++ pointers and references are two different concepts.
>
>>as it is, REFERENCES like POINTERS speak to POINTING TO AN ADDRESS or a
>>COPY
>>OF A VALUE AT AN ADDRESS...which is EXACTLY WHAT A POINTER IS/DOES.
>
> Correct, but that's not what a reference does.
you take all the fun out of this micha! i wanted gosha to say all of that. i
thought his quoted support was lack luster (i guess what i found on the net
was not the same as what he was refering to). ;^)
Re: =& when creating new object
am 24.09.2007 23:37:41 von Steve
"Joe" wrote in message
news:1190669095.585803.46740@d55g2000hsg.googlegroups.com...
> Right to left makes sense. Thanks.
>
> I'm still unsure of cases where I should use & and where I should not,
> but I guess that will come from reading more examples.
you'll often find yourself needing to use them with updating array members.
since php 5 though, i haven't used them too awefully much in new
development. just keep it in the back of your mind. a time will come when
you say, 'hey, i know what i can do here!'
cheers.
Re: =& when creating new object
am 24.09.2007 23:38:21 von Michael Fesser
..oO(Steve)
>"Michael Fesser" wrote in message
>news:60agf3dds15g16okej94srele60sifocfu@4ax.com...
>>
>> Correct, but that's not what a reference does.
>
>you take all the fun out of this micha!
Soooorry.
>i wanted gosha to say all of that.
OK. Gosha! Repeat after me ...
SCNR
Micha
Re: =& when creating new object
am 24.09.2007 23:40:48 von Steve
"Michael Fesser" wrote in message
news:qgbgf3ljt4mdpeto9n4jaigkv460bnn2uu@4ax.com...
> .oO(Steve)
>
>>"Michael Fesser" wrote in message
>>news:60agf3dds15g16okej94srele60sifocfu@4ax.com...
>>>
>>> Correct, but that's not what a reference does.
>>
>>you take all the fun out of this micha!
>
> Soooorry.
>
>>i wanted gosha to say all of that.
>
> OK. Gosha! Repeat after me ...
>
> SCNR
roflmao!
Re: =& when creating new object
am 24.09.2007 23:46:29 von gosha bine
Joe wrote:
> I'm still unsure of cases where I should use & and where I should not,
That's simple. As a rule of thumb, you should not.
Every time you're using & (in php5), you must be doing something wrong.
"Michael Fesser" wrote in message
news:qgbgf3ljt4mdpeto9n4jaigkv460bnn2uu@4ax.com...
> .oO(Steve)
>
>>"Michael Fesser" wrote in message
>>news:60agf3dds15g16okej94srele60sifocfu@4ax.com...
>>>
>>> Correct, but that's not what a reference does.
>>
>>you take all the fun out of this micha!
>
> Soooorry.
>
>>i wanted gosha to say all of that.
>
> OK. Gosha! Repeat after me ...
>
> SCNR
> Micha
micha,
i've been perplexed by this one, since we're talking about &. i think i know
the answer but cannot get confirmation. look at this:
let's say that's an abstract way to build a select option list using
array_walk. the user data argument that i want to use is an array. i want to
make additions to the array, so i use the & instruction. however, i cannot
just pass $options to array walk or it won't work. i think this has to do
with array_walk seeing $options as unitialized...user data is not being
passed a reference. i think what makes it work is the fact that an empty
string gets allocation. by passing an initialized string as part of an
array, optionList, then 'memory' is allocated for both members of
optionList.
does that sound right, or do you have another take on it? just
wondering...i've been puzzled at the explanation for a while.
Re: =& when creating new object
am 24.09.2007 23:59:02 von Steve
"gosha bine" wrote in message
news:46f830bd$0$31120$6e1ede2f@read.cnntp.org...
> Joe wrote:
>> I'm still unsure of cases where I should use & and where I should not,
>
> That's simple. As a rule of thumb, you should not.
> Every time you're using & (in php5), you must be doing something wrong.
bullshit!
that's like me saying, 'every time gosha speaks, what procedes is bullshit'.
however i'd probably be more close to the truth even in my exageration than
you are in yours.
;^)
i gave an example of & being the ONLY way to handle a situation with an
uninitialized array being passed to a function. it don't work without it.
and, it isn't a bug. hmmm, must be good reasons to have it out there.
Re: =& when creating new object
am 25.09.2007 00:13:23 von Michael Fesser
..oO(gosha bine)
>Joe wrote:
>> I'm still unsure of cases where I should use & and where I should not,
>
>That's simple. As a rule of thumb, you should not.
>Every time you're using & (in php5), you must be doing something wrong.
No! References have their uses, even in PHP5. You don't need them for
objects anymore, but in many other cases they can still be necessary.
Micha
Re: =& when creating new object
am 25.09.2007 00:29:48 von Shelly
"gosha bine" wrote in message
news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
> Shelly wrote:
>> "Steve" wrote in message
>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>> "Joe" 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 '
';
>>>
>>> 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
Re: =& when creating new object
am 25.09.2007 00:59:13 von Jerry Stuckle
Shelly wrote:
> "gosha bine" wrote in message
> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>> Shelly wrote:
>>> "Steve" wrote in message
>>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>>> "Joe" 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 '
';
>>>>
>>>> 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".
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: =& when creating new object
am 25.09.2007 01:00:05 von gosha bine
Michael Fesser wrote:
> .oO(gosha bine)
>
>> Joe wrote:
>>> I'm still unsure of cases where I should use & and where I should not,
>> That's simple. As a rule of thumb, you should not.
>> Every time you're using & (in php5), you must be doing something wrong.
>
> No! References have their uses, even in PHP5. You don't need them for
> objects anymore, but in many other cases they can still be necessary.
>
I can't think of any situation where references would be necessary or at
least useful. Would you care to provide some examples?
>Michael Fesser wrote:
>>
>> No! References have their uses, even in PHP5. You don't need them for
>> objects anymore, but in many other cases they can still be necessary.
>
>I can't think of any situation where references would be necessary or at
>least useful. Would you care to provide some examples?
Let's say I have a structure of nested arrays, like a tree. For
convenience reasons I want to access a particular element just by giving
the "path" to it, like "first level/second level/third level". Now the
access function has to tokenize the string and walk down the array tree.
If I would just want to read the value of a particular element, I would
not need any references at all, copies would be fine. But if I want to
change the value, it has to be done in the original array. So the access
function has to keep track of where it actually is in the array - it
always has to point to the current element while walking down the tree.
Micha
Re: =& when creating new object
am 25.09.2007 01:57:46 von Shelly
"Jerry Stuckle" wrote in message
news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@comcast.com...
> Shelly wrote:
>> "gosha bine" wrote in message
>> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>> Shelly wrote:
>>>> "Steve" wrote in message
>>>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>>>> "Joe" 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 '
';
>>>>>
>>>>> 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....".
You say pot-tay-to and I say po-tah-to.
Shelly
Re: =& when creating new object
am 25.09.2007 01:58:43 von gosha bine
Michael Fesser wrote:
> .oO(gosha bine)
>
>> Michael Fesser wrote:
>>> No! References have their uses, even in PHP5. You don't need them for
>>> objects anymore, but in many other cases they can still be necessary.
>> I can't think of any situation where references would be necessary or at
>> least useful. Would you care to provide some examples?
>
> Let's say I have a structure of nested arrays, like a tree. For
> convenience reasons I want to access a particular element just by giving
> the "path" to it, like "first level/second level/third level". Now the
> access function has to tokenize the string and walk down the array tree.
>
> If I would just want to read the value of a particular element, I would
> not need any references at all, copies would be fine. But if I want to
> change the value, it has to be done in the original array. So the access
> function has to keep track of where it actually is in the array - it
> always has to point to the current element while walking down the tree.
>
Well, for this particular task (and if, for some reason, you have to
reinvent the DOMDocument wheel) the code can look like this:
class node {
protected $name;
protected $value;
...
}
$doc = new node('html');
$doc->append('body')->append('div')->append('span');
$doc->find_node('/body/div/span')->set_value('hello');
$doc->replace_node('/body/div/span', new node('h1'));
I still fail to see how references can be useful here.
Shelly wrote:
> "Jerry Stuckle" wrote in message
> news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@comcast.com...
>> Shelly wrote:
>>> "gosha bine" wrote in message
>>> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>>> Shelly wrote:
>>>>> "Steve" wrote in message
>>>>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>>>>> "Joe" 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 '
';
>>>>>>
>>>>>> 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
==================
Re: =& when creating new object
am 25.09.2007 05:07:09 von Steve
"gosha bine" wrote in message
news:46f84fbb$0$31121$6e1ede2f@read.cnntp.org...
> Michael Fesser wrote:
>> .oO(gosha bine)
>>
>>> Michael Fesser wrote:
>>>> No! References have their uses, even in PHP5. You don't need them for
>>>> objects anymore, but in many other cases they can still be necessary.
>>> I can't think of any situation where references would be necessary or at
>>> least useful. Would you care to provide some examples?
>>
>> Let's say I have a structure of nested arrays, like a tree. For
>> convenience reasons I want to access a particular element just by giving
>> the "path" to it, like "first level/second level/third level". Now the
>> access function has to tokenize the string and walk down the array tree.
>>
>> If I would just want to read the value of a particular element, I would
>> not need any references at all, copies would be fine. But if I want to
>> change the value, it has to be done in the original array. So the access
>> function has to keep track of where it actually is in the array - it
>> always has to point to the current element while walking down the tree.
>>
>
> Well, for this particular task (and if, for some reason, you have to
> reinvent the DOMDocument wheel) the code can look like this:
yes, well we quite got off topic by claiming that now in php 5 referencing
literally is not needed. and that, i believe, was a tangent that you
provided...and then boldly asked for examples of, which both micha and i
gave to you. and now, you want to get back into an isolated instance that
doesn't require it. we call that a 'strawman'.
if you keep making sweeping statements that are not true about php, i'll be
happy to continually thwart them. i suppose that's why you killfile me. and
even though you may, everyone else can read all the posts. that kind of puts
you at a disadvantage. ;^)
Re: =& when creating new object
am 25.09.2007 05:25:44 von Steve
"Jerry Stuckle" wrote in message
news:VLWdnbBpwNDwzmXbnZ2dnUVZ_gmdnZ2d@comcast.com...
> Shelly wrote:
>> "Jerry Stuckle" wrote in message
>> news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@comcast.com...
>>> Shelly wrote:
>>>> "gosha bine" wrote in message
>>>> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>>>> Shelly wrote:
>>>>>> "Steve" wrote in message
>>>>>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>>>>>> "Joe" 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 '
';
>>>>>>>
>>>>>>> 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.
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.
> 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...
but, who gives a shit 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 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.
> *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.
> 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.
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 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.
>> 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?
;^)
Re: =& when creating new object
am 25.09.2007 12:43:35 von Jerry Stuckle
Steve wrote:
> "Jerry Stuckle" wrote in message
> news:VLWdnbBpwNDwzmXbnZ2dnUVZ_gmdnZ2d@comcast.com...
>> Shelly wrote:
>>> "Jerry Stuckle" wrote in message
>>> news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@comcast.com...
>>>> Shelly wrote:
>>>>> "gosha bine" wrote in message
>>>>> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>>>>> Shelly wrote:
>>>>>>> "Steve" wrote in message
>>>>>>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>>>>>>> "Joe" 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 '
';
>>>>>>>>
>>>>>>>> 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".
> 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 shit 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.
>>>
>>> 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.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: =& when creating new object
am 25.09.2007 15:35:05 von Steve
"Jerry Stuckle" wrote in message
news:i6WdnSgveO9ee2XbnZ2dnUVZ_gadnZ2d@comcast.com...
> Steve wrote:
>> "Jerry Stuckle" wrote in message
>> news:VLWdnbBpwNDwzmXbnZ2dnUVZ_gmdnZ2d@comcast.com...
>>> Shelly wrote:
>>>> "Jerry Stuckle" wrote in message
>>>> news:J_ednRJzLNIw3GXbnZ2dnUVZ_hGdnZ2d@comcast.com...
>>>>> Shelly wrote:
>>>>>> "gosha bine" wrote in message
>>>>>> news:46f823bf$0$31121$6e1ede2f@read.cnntp.org...
>>>>>>> Shelly wrote:
>>>>>>>> "Steve" wrote in message
>>>>>>>> news:r6VJi.36$Nr6.32@newsfe05.lga...
>>>>>>>>> "Joe" 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 '
';
>>>>>>>>>
>>>>>>>>> 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 shit 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!
Re: =& when creating new object
am 26.09.2007 06:58:11 von Jerry Stuckle
Steve wrote:
> "Jerry Stuckle" wrote in message
>> 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?
>
Actually, yes they do. Of course, they're on a team with other
programmers. But the idea of the course is to give them enough of the
syntax, structure, functions, etc. so they can program. No, they're not
going to be experts, and they will make mistakes. But they are productive.
> 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!
>
>
The difference being that in a University, you're paying them to take
the classes, and they don't care what you write outside of class.
Additionally, in a university environment you get a lot more theory,
which may or may not be applicable in a particular business environment.
In a business environment, the company is paying the student to take the
class, as well as the instructor. The company also cares about what the
student will be writing after class is over. It is to the company's
benefit for the employee to learn as much as possible as quickly as
possible. And every bit of it is applicable to the business environment
the student is in.
It's virtually impossible to sell more than a 5 day class to a
corporation. They don't want their employees out any longer than that,
and they don't want to pay for more.
I'm not the only one who does this. Look around at the corporate
training companies. Most of them do C, C++, Java, etc. as five day courses.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================