Sure, but you defined ptr outside of the loop. Who defined $object outside of the loop in the PHP example?
Also note that, in C, you have to explicitly dereference pointers:
ptr = 42;
You should get a warning from your compiler if you try that. PHP effectively goes ahead and changes it to:
*ptr = 42;
Perl requires you to explicitly dereference references as well:
@array = ('c', 'c++', 'java', 'perl');
$item = \$array[3];
$item = 'php'; # Does NOT replace the item in the array
foreach $item (@array) {
print $item . "\n";
}
PHP is weird in this regard: if you ever assign a reference to a variable, later assignments go through that reference automatically. To convert the variable back to a normal variable, you have to unset it. I don't know of another language that acts like this. Can you think of one?
Combine that with the lack of block scope, and you get surprises like the example BadCRC linked.
You think PHP is weird because you don't understand references. Reference is not a pointer, PHP doesn't have pointers. Reference is variable binding. $a =& $b is not an assignment. It is a binding - it means $a and $b are now bound to the same variable/value.
No, I do understand references. I'm just trying to explain how references in PHP are very, very different from references or pointers in any other language I'm aware of, and how they could be confusing to people who know other languages well but are less experienced with PHP.
Perl does have a similar concept, but it calls it aliasing. Perl references behave more like what you'd expect coming from other languages.
I wish PHP had called its references something else. They're not really references at all.
Just because I think PHP references are weird doesn't mean I don't know how they work. It does make me wonder, though, why PHP references work the way they do. What's the benefit of doing it this way instead of the way just about every other language ever does it?
Why PHP references should be like pointers? It is "confusing" only if you have wrong preconception about how PHP references should be. If you let it go and learn what they actually are, there's nothing confusing about it. Just don't let you preconceived notion about what it "should" be block your learning about what it actually is.
Most of "other languages" similar to PHP don't even have concept close to references, except for C++. Similarity with C++ may be briefly confusing, but it's not unheard of that in different languages concepts meant to achieve the same thing work differently. Expecting PHP would match C++ in every detail or would have to invent completely new terminology altogether makes little sense.
>> What's the benefit of doing it this way instead of the way just about every other language ever does it?
"Every other language ever" doesn't do it in any particular way. Perl has pointer-like references and they are nightmarish to work with. Languages like Ruby or Java pass everything by value or by object reference depending on how you look on it, since everything is an object. Some don't have the concept of references all. Low-level languages like C/C++ have pointers. In some languages variables are not mutable, so the whole question is moot. Saying that "every other language ever" does some specific thing in this regard and only PHP does it differently is meaningless - different languages do it completely different, and PHP has its own way. It doesn't match your favorite one - fine, everybody is entitled to have one's favorite ways, but that does not make PHP "weird" or wrong in any way, just as it doesn't make C, Java, Python or Perl wrong.
PHP has references, because once upon a time it didn't have pointers. If the only way to pass pointers is by value, then you are severely limited. So it got references, following (I assume intentionally) Perl's syntax and semantics, though with its own reference counting foibles.
Then it got pointers, and references suddenly weren't needed that much anymore. But it kept the old semantics, and still needs references for passing arrays around, since arrays are not objects, and PHP assignment is still by copy.
WTF. Sometimes I wonder if I'm being unfairly prejudiced by not learning PHP myself and making my own decision about it but things like this make me change my mind.
You are completely correct - you are being unfairly prejudiced by judging PHP on explanations of people that do not understand it instead of learning it. If you learned it, you would know references work exactly like they supposed to, and do exactly what they are meant to do - just because they are not, as parent assumes, pointers, he misunderstands them and thinks they are weird. Moreover, he thinks since PHP in not like C and does not have C-like pointers PHP is weird. Please read http://us2.php.net/references if you want real information instead of collection of misunderstanding.
I think the whole point here is why does PHP even have references like that. Right now the only mainstream language I know that does that is C++ and well, its C++ and mutable references are actually not frequently used in practice. Most other languages stay with pass by value most of the time and PHP references kind of look like a leaky abstraction from the underlying C implementation...
C++ references and PHP references are completely different beasts. PHP references like Unix hardlinks, for example.
As for why they exist - they make doing some things easier and some use cases easier to program, without introducing concepts like pointers which are completely foreign to language like PHP. Could we do without them? Yes, probably. That's only one way to solve it, there can be others.
And in C++, a variable is either declared as a reference and acts like one, or it's declared as a value and acts like one. It can't change behavior halfway through a function.
(Well, ignoring operator overloading anyway… you could conceivably import PHP's concept of variables and references into C++.)
I use references for passing around large arrays of data for instance, rather than having copies made all over the place (class instances are passed by reference no matter what you do).
What I meant is that people usually pass things via a pointer (that needs to be explicitly dereferrenced) or via const references (where there is no mutation to worry about). Mutable references are rarer and usually they are function arguments and are relatively "obvious"
I thought this for a long time as well. I always enjoyed ragging on PHP but wondered if it was just because I wasn't familiar with it.
A few months ago, I got the opportunity to do a little PHP programming. I was wrong. I didn't have a bad impression of PHP simply out of ignorance. In fact, my opinion of the language is now much lower than before. I honestly can't conceive how anyone could legitimately defend it for anything other than its ubiquity.
Also note that, in C, you have to explicitly dereference pointers:
You should get a warning from your compiler if you try that. PHP effectively goes ahead and changes it to: Perl requires you to explicitly dereference references as well: PHP is weird in this regard: if you ever assign a reference to a variable, later assignments go through that reference automatically. To convert the variable back to a normal variable, you have to unset it. I don't know of another language that acts like this. Can you think of one?Combine that with the lack of block scope, and you get surprises like the example BadCRC linked.