By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

=& when creating new object

P: n/a
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);

What exactly is the =&, and why is it different from = ?

Sep 24 '07 #1
Share this Question
Share on Google+
32 Replies


P: n/a
..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
Sep 24 '07 #2

P: n/a

"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.com...
>I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):

$wakka =& new Wakka($wakkaConfig);

What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory location
where the value is stored'. without the &, it means 'a copy of the value of
the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and $reference
point to the same memory location. changing one but using the other makes no
difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting them
so. once you modify one of them, you have done so idependently of the
other - changing one has no effect on the other.

hope that makes more sense.
Sep 24 '07 #3

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:d5********************************@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?
Sep 24 '07 #4

P: n/a

"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
>
"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.com...
>>I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):

$wakka =& new Wakka($wakkaConfig);

What exactly is the =&, and why is it different from = ?

well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory location
where the value is stored'. without the &, it means 'a copy of the value
of the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and $reference
point to the same memory location. changing one but using the other makes
no difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting them
so. once you modify one of them, you have done so idependently of the
other - changing one has no effect on the other.

hope that makes more sense.
That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.

Shelly
Sep 24 '07 #5

P: n/a
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" <no....@example.comwrote:
"Michael Fesser" <neti...@gmx.dewrote in message

news:d5********************************@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?

Sep 24 '07 #6

P: n/a

"Shelly" <sh************@asap-consult.comwrote in message
news:13*************@corp.supernews.com...
>
"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
>>
"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegro ups.com...
>>>I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):

$wakka =& new Wakka($wakkaConfig);

What exactly is the =&, and why is it different from = ?

well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory location
where the value is stored'. without the &, it means 'a copy of the value
of the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and $reference
point to the same memory location. changing one but using the other makes
no difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting
them so. once you modify one of them, you have done so idependently of
the other - changing one has no effect on the other.

hope that makes more sense.

That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.
and i bet it opened up an entirely new world to your mind to set off
on...one that is constructed much more simply that without it, right? yeah,
that was a big deal for me too...going from BASIC to C. before BASIC, i
worked directly with memory registers in PLC's. i just thought i'd have to
give up that ability when i got into BASIC. then i saw the construct and an
example in a C programming book. BINGO, i had it all again! what's funny is
the book explained it poorly (too verbose), however the best example i found
yeeeaaaarrrrssss later was on php.net in their docs. a single four line
example that showed what was going on.
Sep 24 '07 #7

P: n/a
Shelly wrote:
"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
>"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegro ups.com...
>>I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):

$wakka =& new Wakka($wakkaConfig);

What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory location
where the value is stored'. without the &, it means 'a copy of the value
of the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and $reference
point to the same memory location. changing one but using the other makes
no difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting them
so. once you modify one of them, you have done so idependently of the
other - changing one has no effect on the other.

hope that makes more sense.

That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.

Shelly

php references have nothing to do with C-alike pointers.

Please read the chapter called "references are not pointers" in the manual.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 24 '07 #8

P: n/a
..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.

Micha
Sep 24 '07 #9

P: n/a

"Joe" <jo*@faceh.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.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.
Sep 24 '07 #10

P: n/a

"Joe" <jo*@faceh.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.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
Sep 24 '07 #11

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:1p********************************@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. ;^)
Sep 24 '07 #12

P: n/a

"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org...
Shelly wrote:
>"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
>>"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegr oups.com...
I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):

$wakka =& new Wakka($wakkaConfig);

What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory
location where the value is stored'. without the &, it means 'a copy of
the value of the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and
$reference point to the same memory location. changing one but using the
other makes no difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting
them so. once you modify one of them, you have done so idependently of
the other - changing one has no effect on the other.

hope that makes more sense.

That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.

Shelly

php references have nothing to do with C-alike pointers.

Please read the chapter called "references are not pointers" in the
manual.
'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.
Sep 24 '07 #13

P: n/a
..oO(Steve)
>"gosha bine" <st********@gmail.comwrote in message
news:46***********************@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
Sep 24 '07 #14

P: n/a
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" <no....@example.comwrote:
"gosha bine" <stereof...@gmail.comwrote in message

news:46***********************@read.cnntp.org...
Shelly wrote:
"Steve" <no....@example.comwrote in message
news:r6*************@newsfe05.lga...
"Joe" <j...@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegr oups.com...
I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):
>>$wakka =& new Wakka($wakkaConfig);
>>What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory
location where the value is stored'. without the &, it means 'a copy of
the value of the variable'.
>clear as mud? rather than explain memory, let me have you do this:
>$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';
>unset($variable);
unset($reference);
>$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';
>the first example, BY REFERENCE, means that both $variable and
$reference point to the same memory location. changing one but using the
other makes no difference - using either will have the same effect.
>the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting
them so. once you modify one of them, you have done so idependently of
the other - changing one has no effect on the other.
>hope that makes more sense.
That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.
Shelly
php references have nothing to do with C-alike pointers.
Please read the chapter called "references are not pointers" in the
manual.

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

Sep 24 '07 #15

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:60********************************@4ax.com...
.oO(Steve)
>>"gosha bine" <st********@gmail.comwrote in message
news:46***********************@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). ;^)
Sep 24 '07 #16

P: n/a

"Joe" <jo*@faceh.comwrote in message
news:11*********************@d55g2000hsg.googlegro ups.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.
Sep 24 '07 #17

P: n/a
..oO(Steve)
>"Michael Fesser" <ne*****@gmx.dewrote in message
news:60********************************@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
Sep 24 '07 #18

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:qg********************************@4ax.com...
.oO(Steve)
>>"Michael Fesser" <ne*****@gmx.dewrote in message
news:60********************************@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!
Sep 24 '07 #19

P: n/a
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.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 24 '07 #20

P: n/a

"Michael Fesser" <ne*****@gmx.dewrote in message
news:qg********************************@4ax.com...
.oO(Steve)
>>"Michael Fesser" <ne*****@gmx.dewrote in message
news:60********************************@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:

<?
function buildOptionList($display, $value, &$options)
{
$options[1][] = '<option value="' . $value . '" ' .
($value == $options[0] ? 'selected' : '') .
'>' . $display . '</option>';
}
$categories = array(
'BODY' ='BODY' ,
'FRAME' ='FRAME' ,
'MECHANICAL' ='MECHANICAL' ,
'PAINT' ='PAINT'
);
$options = array();
$optionList = array('MECHANICAL', &$options);
array_walk($categories, 'buildOptionList', $optionList);
?>

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.
Sep 24 '07 #21

P: n/a

"gosha bine" <st********@gmail.comwrote in message
news:46***********************@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.
Sep 24 '07 #22

P: n/a
..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
Sep 24 '07 #23

P: n/a

"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org...
Shelly wrote:
>"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
>>"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googlegr oups.com...
I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):

$wakka =& new Wakka($wakkaConfig);

What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory
location where the value is stored'. without the &, it means 'a copy of
the value of the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and
$reference point to the same memory location. changing one but using the
other makes no difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting
them so. once you modify one of them, you have done so idependently of
the other - changing one has no effect on the other.

hope that makes more sense.

That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.

Shelly

php references have nothing to do with C-alike pointers.

Please read the chapter called "references are not pointers" in the
manual.
Yeah, yeah. We still are talking about address-of and not value-of. So
there is no explicit pointer variable as there is in C. So what?

Shelly
Sep 24 '07 #24

P: n/a
Shelly wrote:
"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org...
>Shelly wrote:
>>"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.googleg roups.com...
I am just starting to use Object Oriented PHP coding, and I am seeing
quite often the following (this example taken from a wiki):
>
$wakka =& new Wakka($wakkaConfig);
>
What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory
location where the value is stored'. without the &, it means 'a copy of
the value of the variable'.

clear as mud? rather than explain memory, let me have you do this:

$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

unset($variable);
unset($reference);

$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';

the first example, BY REFERENCE, means that both $variable and
$reference point to the same memory location. changing one but using the
other makes no difference - using either will have the same effect.

the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting
them so. once you modify one of them, you have done so idependently of
the other - changing one has no effect on the other.

hope that makes more sense.
That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C, coming
from a Fortran background as I did.

Shelly
php references have nothing to do with C-alike pointers.

Please read the chapter called "references are not pointers" in the
manual.

Yeah, yeah. We still are talking about address-of and not value-of. So
there is no explicit pointer variable as there is in C. So what?

Shelly

No, it's not address-of, either in PHP or C++. It's more "alias-of".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 24 '07 #25

P: n/a
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?
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 24 '07 #26

P: n/a
..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.

Micha
Sep 24 '07 #27

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:J_******************************@comcast.com. ..
Shelly wrote:
>"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org.. .
>>Shelly wrote:
"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
"Joe" <jo*@faceh.comwrote in message
news:11*********************@22g2000hsm.google groups.com...
>I am just starting to use Object Oriented PHP coding, and I am seeing
>quite often the following (this example taken from a wiki):
>>
>$wakka =& new Wakka($wakkaConfig);
>>
>What exactly is the =&, and why is it different from = ?
well, better syntax would have helped. it should read ' $something =
&$variable'. in this context, & means 'a reference to the memory
location where the value is stored'. without the &, it means 'a copy
of the value of the variable'.
>
clear as mud? rather than explain memory, let me have you do this:
>
$variable = 'hello';
$reference = &$variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';
>
unset($variable);
unset($reference);
>
$variable = 'hello';
$reference = $variable;
$variable = 'world';
echo '<pre>' . print_r($reference, true) . '</pre>';
$reference = 'good-bye';
echo '<pre>' . print_r($variable, true) . '</pre>';
>
the first example, BY REFERENCE, means that both $variable and
$reference point to the same memory location. changing one but using
the other makes no difference - using either will have the same
effect.
>
the second, BY VALUE, means that each variable points to two different
locations in memory. the only time they will be equal is when setting
them so. once you modify one of them, you have done so idependently of
the other - changing one has no effect on the other.
>
hope that makes more sense.
That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C,
coming from a Fortran background as I did.

Shelly
php references have nothing to do with C-alike pointers.

Please read the chapter called "references are not pointers" in the
manual.

Yeah, yeah. We still are talking about address-of and not value-of. So
there is no explicit pointer variable as there is in C. So what?

Shelly

No, it's not address-of, either in PHP or C++. It's more "alias-of".
....and by alias of you mean another way of referencing the same place in
memory that contains the value as the thing it is aliasing. --- aka
address-of. Change the value contained in the "reference" and the value of
the "referenced" changes. Change the value located at the "address of" and
the value of the other variable changes. Six of one, a half dozen of the
other. *ptr->foo=junk does the same thing as saying foo=junk. Also
fee=&foo and *fee=junk does the same thing. (It has been about seven years
six I did any C coding (so my memory of exact syntax may be a little off),
but it all comes down to the Bard -- "a rose by any other name....".

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

Shelly
Sep 25 '07 #28

P: n/a
Shelly wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:J_******************************@comcast.com. ..
>Shelly wrote:
>>"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org. ..
Shelly wrote:
"Steve" <no****@example.comwrote in message
news:r6*************@newsfe05.lga...
>"Joe" <jo*@faceh.comwrote in message
>news:11*********************@22g2000hsm.googl egroups.com...
>>I am just starting to use Object Oriented PHP coding, and I am seeing
>>quite often the following (this example taken from a wiki):
>>>
>>$wakka =& new Wakka($wakkaConfig);
>>>
>>What exactly is the =&, and why is it different from = ?
>well, better syntax would have helped. it should read ' $something =
>&$variable'. in this context, & means 'a reference to the memory
>location where the value is stored'. without the &, it means 'a copy
>of the value of the variable'.
>>
>clear as mud? rather than explain memory, let me have you do this:
>>
>$variable = 'hello';
>$reference = &$variable;
>$variable = 'world';
>echo '<pre>' . print_r($reference, true) . '</pre>';
>$reference = 'good-bye';
>echo '<pre>' . print_r($variable, true) . '</pre>';
>>
>unset($variable);
>unset($reference);
>>
>$variable = 'hello';
>$reference = $variable;
>$variable = 'world';
>echo '<pre>' . print_r($reference, true) . '</pre>';
>$reference = 'good-bye';
>echo '<pre>' . print_r($variable, true) . '</pre>';
>>
>the first example, BY REFERENCE, means that both $variable and
>$reference point to the same memory location. changing one but using
>the other makes no difference - using either will have the same
>effect.
>>
>the second, BY VALUE, means that each variable points to two different
>locations in memory. the only time they will be equal is when setting
>them so. once you modify one of them, you have done so idependently of
>the other - changing one has no effect on the other.
>>
>hope that makes more sense.
That was probably the single most unique new concept (pointers and
address-of) I had conquer when (os so many years ago) I learned C,
coming from a Fortran background as I did.
>
Shelly
php references have nothing to do with C-alike pointers.

Please read the chapter called "references are not pointers" in the
manual.

Yeah, yeah. We still are talking about address-of and not value-of. So
there is no explicit pointer variable as there is in C. So what?

Shelly
No, it's not address-of, either in PHP or C++. It's more "alias-of".

...and by alias of you mean another way of referencing the same place in
memory that contains the value as the thing it is aliasing. --- aka
address-of. Change the value contained in the "reference" and the value of
the "referenced" changes. Change the value located at the "address of" and
the value of the other variable changes. Six of one, a half dozen of the
other. *ptr->foo=junk does the same thing as saying foo=junk. Also
fee=&foo and *fee=junk does the same thing. (It has been about seven years
six I did any C coding (so my memory of exact syntax may be a little off),
but it all comes down to the Bard -- "a rose by any other name....".
Well, of course everything in the computer has an address. But you
should not consider it an address in PHP (PHP doesn't HAVE addresses).
It might be referencing a hash value in a table, for instance, and that
table may have no fixed address.

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

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

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

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

I could continue - but there are significant differences between
pointers in references in C++.
You say pot-tay-to and I say po-tah-to.

Shelly

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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 25 '07 #29

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:VL******************************@comcast.com. ..
Shelly wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:J_******************************@comcast.com ...
>>Shelly wrote:
"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org.. .
Shelly wrote:
>"Steve" <no****@example.comwrote in message
>news:r6*************@newsfe05.lga...
>>"Joe" <jo*@faceh.comwrote in message
>>news:11*********************@22g2000hsm.goog legroups.com...
>>>I am just starting to use Object Oriented PHP coding, and I am
>>>seeing
>>>quite often the following (this example taken from a wiki):
>>>>
>>>$wakka =& new Wakka($wakkaConfig);
>>>>
>>>What exactly is the =&, and why is it different from = ?
>>well, better syntax would have helped. it should read ' $something =
>>&$variable'. in this context, & means 'a reference to the memory
>>location where the value is stored'. without the &, it means 'a copy
>>of the value of the variable'.
>>>
>>clear as mud? rather than explain memory, let me have you do this:
>>>
>>$variable = 'hello';
>>$reference = &$variable;
>>$variable = 'world';
>>echo '<pre>' . print_r($reference, true) . '</pre>';
>>$reference = 'good-bye';
>>echo '<pre>' . print_r($variable, true) . '</pre>';
>>>
>>unset($variable);
>>unset($reference);
>>>
>>$variable = 'hello';
>>$reference = $variable;
>>$variable = 'world';
>>echo '<pre>' . print_r($reference, true) . '</pre>';
>>$reference = 'good-bye';
>>echo '<pre>' . print_r($variable, true) . '</pre>';
>>>
>>the first example, BY REFERENCE, means that both $variable and
>>$reference point to the same memory location. changing one but using
>>the other makes no difference - using either will have the same
>>effect.
>>>
>>the second, BY VALUE, means that each variable points to two
>>different locations in memory. the only time they will be equal is
>>when setting them so. once you modify one of them, you have done so
>>idependently of the other - changing one has no effect on the other.
>>>
>>hope that makes more sense.
>That was probably the single most unique new concept (pointers and
>address-of) I had conquer when (os so many years ago) I learned C,
>coming from a Fortran background as I did.
>>
>Shelly
php references have nothing to do with C-alike pointers.
>
Please read the chapter called "references are not pointers" in the
manual.
>
Yeah, yeah. We still are talking about address-of and not value-of.
So there is no explicit pointer variable as there is in C. So what?

Shelly
No, it's not address-of, either in PHP or C++. It's more "alias-of".

...and by alias of you mean another way of referencing the same place in
memory that contains the value as the thing it is aliasing. --- aka
address-of. Change the value contained in the "reference" and the value
of the "referenced" changes. Change the value located at the "address
of" and the value of the other variable changes. Six of one, a half
dozen of the other. *ptr->foo=junk does the same thing as saying
foo=junk. Also fee=&foo and *fee=junk does the same thing. (It has
been about seven years six I did any C coding (so my memory of exact
syntax may be a little off), but it all comes down to the Bard -- "a
rose by any other name....".

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

;^)
Sep 25 '07 #30

P: n/a
Steve wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:VL******************************@comcast.com. ..
>Shelly wrote:
>>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:J_******************************@comcast.co m...
Shelly wrote:
"gosha bine" <st********@gmail.comwrote in message
news:46***********************@read.cnntp.org. ..
>Shelly wrote:
>>"Steve" <no****@example.comwrote in message
>>news:r6*************@newsfe05.lga...
>>>"Joe" <jo*@faceh.comwrote in message
>>>news:11*********************@22g2000hsm.goo glegroups.com...
>>>>I am just starting to use Object Oriented PHP coding, and I am
>>>>seeing
>>>>quite often the following (this example taken from a wiki):
>>>>>
>>>>$wakka =& new Wakka($wakkaConfig);
>>>>>
>>>>What exactly is the =&, and why is it different from = ?
>>>well, better syntax would have helped. it should read ' $something =
>>>&$variable'. in this context, & means 'a reference to the memory
>>>location where the value is stored'. without the &, it means 'a copy
>>>of the value of the variable'.
>>>>
>>>clear as mud? rather than explain memory, let me have you do this:
>>>>
>>>$variable = 'hello';
>>>$reference = &$variable;
>>>$variable = 'world';
>>>echo '<pre>' . print_r($reference, true) . '</pre>';
>>>$reference = 'good-bye';
>>>echo '<pre>' . print_r($variable, true) . '</pre>';
>>>>
>>>unset($variable);
>>>unset($reference);
>>>>
>>>$variable = 'hello';
>>>$reference = $variable;
>>>$variable = 'world';
>>>echo '<pre>' . print_r($reference, true) . '</pre>';
>>>$reference = 'good-bye';
>>>echo '<pre>' . print_r($variable, true) . '</pre>';
>>>>
>>>the first example, BY REFERENCE, means that both $variable and
>>>$reference point to the same memory location. changing one but using
>>>the other makes no difference - using either will have the same
>>>effect.
>>>>
>>>the second, BY VALUE, means that each variable points to two
>>>different locations in memory. the only time they will be equal is
>>>when setting them so. once you modify one of them, you have done so
>>>idependently of the other - changing one has no effect on the other.
>>>>
>>>hope that makes more sense.
>>That was probably the single most unique new concept (pointers and
>>address-of) I had conquer when (os so many years ago) I learned C,
>>coming from a Fortran background as I did.
>>>
>>Shelly
>php references have nothing to do with C-alike pointers.
>>
>Please read the chapter called "references are not pointers" in the
>manual.
>>
Yeah, yeah. We still are talking about address-of and not value-of.
So there is no explicit pointer variable as there is in C. So what?
>
Shelly
No, it's not address-of, either in PHP or C++. It's more "alias-of".
...and by alias of you mean another way of referencing the same place in
memory that contains the value as the thing it is aliasing. --- aka
address-of. Change the value contained in the "reference" and the value
of the "referenced" changes. Change the value located at the "address
of" and the value of the other variable changes. Six of one, a half
dozen of the other. *ptr->foo=junk does the same thing as saying
foo=junk. Also fee=&foo and *fee=junk does the same thing. (It has
been about seven years six I did any C coding (so my memory of exact
syntax may be a little off), but it all comes down to the Bard -- "a
rose by any other name....".
Well, of course everything in the computer has an address. But you should
not consider it an address in PHP (PHP doesn't HAVE addresses). It might
be referencing a hash value in a table, for instance, and that table may
have no fixed address.

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.
js*******@attglobal.net
==================
Sep 25 '07 #31

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:i6******************************@comcast.com. ..
Steve wrote:
>"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:VL******************************@comcast.com ...
>>Shelly wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:J_******************************@comcast.c om...
Shelly wrote:
>"gosha bine" <st********@gmail.comwrote in message
>news:46***********************@read.cnntp.org ...
>>Shelly wrote:
>>>"Steve" <no****@example.comwrote in message
>>>news:r6*************@newsfe05.lga...
>>>>"Joe" <jo*@faceh.comwrote in message
>>>>news:11*********************@22g2000hsm.go oglegroups.com...
>>>>>I am just starting to use Object Oriented PHP coding, and I am
>>>>>seeing
>>>>>quite often the following (this example taken from a wiki):
>>>>>>
>>>>>$wakka =& new Wakka($wakkaConfig);
>>>>>>
>>>>>What exactly is the =&, and why is it different from = ?
>>>>well, better syntax would have helped. it should read ' $something
>>>>= &$variable'. in this context, & means 'a reference to the memory
>>>>location where the value is stored'. without the &, it means 'a
>>>>copy of the value of the variable'.
>>>>>
>>>>clear as mud? rather than explain memory, let me have you do this:
>>>>>
>>>>$variable = 'hello';
>>>>$reference = &$variable;
>>>>$variable = 'world';
>>>>echo '<pre>' . print_r($reference, true) . '</pre>';
>>>>$reference = 'good-bye';
>>>>echo '<pre>' . print_r($variable, true) . '</pre>';
>>>>>
>>>>unset($variable);
>>>>unset($reference);
>>>>>
>>>>$variable = 'hello';
>>>>$reference = $variable;
>>>>$variable = 'world';
>>>>echo '<pre>' . print_r($reference, true) . '</pre>';
>>>>$reference = 'good-bye';
>>>>echo '<pre>' . print_r($variable, true) . '</pre>';
>>>>>
>>>>the first example, BY REFERENCE, means that both $variable and
>>>>$reference point to the same memory location. changing one but
>>>>using the other makes no difference - using either will have the
>>>>same effect.
>>>>>
>>>>the second, BY VALUE, means that each variable points to two
>>>>different locations in memory. the only time they will be equal is
>>>>when setting them so. once you modify one of them, you have done
>>>>so idependently of the other - changing one has no effect on the
>>>>other.
>>>>>
>>>>hope that makes more sense.
>>>That was probably the single most unique new concept (pointers and
>>>address-of) I had conquer when (os so many years ago) I learned C,
>>>coming from a Fortran background as I did.
>>>>
>>>Shelly
>>php references have nothing to do with C-alike pointers.
>>>
>>Please read the chapter called "references are not pointers" in the
>>manual.
>>>
>Yeah, yeah. We still are talking about address-of and not value-of.
>So there is no explicit pointer variable as there is in C. So what?
>>
>Shelly
No, it's not address-of, either in PHP or C++. It's more "alias-of".
...and by alias of you mean another way of referencing the same place
in memory that contains the value as the thing it is aliasing. --- aka
address-of. Change the value contained in the "reference" and the
value of the "referenced" changes. Change the value located at the
"address of" and the value of the other variable changes. Six of one,
a half dozen of the other. *ptr->foo=junk does the same thing as
saying foo=junk. Also fee=&foo and *fee=junk does the same thing.
(It has been about seven years six I did any C coding (so my memory of
exact syntax may be a little off), but it all comes down to the Bard --
"a rose by any other name....".

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

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!
Sep 25 '07 #32

P: n/a
Steve wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote 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.
js*******@attglobal.net
==================
Sep 26 '07 #33

This discussion thread is closed

Replies have been disabled for this discussion.