Hi,
This is nonsense. Calling the deigners of Java, Perl, C# etc. a bunch of people with no good reasons is just silly.
I didn't - you've misunderstood me.
They are not pointers.
Well, if you read that article and understand what it's saying I think you'll find that they are.
I think you're making the incorrect assumption that C/C++'s implementation of pointers are the only possible way of having pointers.
Object handles are much more like file handles -- just an ID.
Well, that's exactly what a pointer is.
A pointer contains an ID (usually a memory address, but doesn't have to be) that provides the "location" of a value. That ID can be copied any number of times. Every copy provides the same location to the same value.
The fact that the PHP code I've provided is identical (except for syntax details, of course) to the C++ code, is pretty conclusive.
FWIW, here is the C++ code.
Code: Select all
#include <iostream>
#include <string>
using namespace std;
class Dog
{
public:
string Name;
Dog(string name)
{
Name = name;
}
string GetName()
{
return (Name);
}
};
void Assign(Dog* p)
{
p = new Dog("Max");
}
void AssignName(Dog* p)
{
p->Name = "Max";
}
main()
{
Dog* foo;
foo = new Dog("Spot");
Assign(foo);
cout << foo->GetName() << endl;
cout << "If that said 'Spot', then PHP object references are like C++ pointers" << endl;
AssignName(foo);
cout << foo->GetName() << endl;
cout << "If that said 'Max', then PHP object references are like C++ pointers" << endl;
}
The bottom line is this: PHP object variable passing works
exactly like C++ pointer passing.
"If it walks like a duck and quacks like duck, it's probably a duck."
They are not pointers.
OK, that's your assertion and the assertion in the PHP documentation. Can you explain in what way PHP object variables are not pointers (except for pointer arithmetic which is irrelevant)?
The point is the PHP developers have actually got this right in the change from PHP4 to PHP5.
In PHP4, the object variables worked like pointers (call them references if you want)
except when they were passed as parameters to functions. That exception was plain wrong (IMHO).
In PHP5, object variables are now consistent - that exception doesn't exist.
So, I started this thread confused about why they changed the semantics from PHP4 to PHP5, and now I understand exactly. If you're going to design your language so that object variables are pointers (whatever you want to call them), then they have to be pointers consistently - not just some of the time.
The argument that "it's more efficient" is completely spurious, and, as I said before, nothing to do with the design of the language. Efficiency comes (mainly) from implementation, not from particular semantic choices in language design.
And as a last point, if you add this PHP code to my example:
Code: Select all
function RefAssign(&$p)
{
$p = new Dog("Gypsy");
}
RefAssign($foo);
echo "<p>".$foo->GetName();
echo "<p>"."If that said 'Gypsy', then explicit references to object variables in PHP are like C++ references";
it shows that an explicit reference to an object variable is exactly like a reference in C++:
Code: Select all
void RefAssign(Dog& p)
{
p = *(new Dog("Gypsy"));
}
// add this to main()
RefAssign(*foo);
cout << foo->GetName() << endl;
cout << "If that said 'Gypsy', then explicit references to object variables in PHP are like C++ references" << endl;
regards,
RR