note 55469 added to language.references.arent

From: Date: Thu, 04 Aug 2005 12:55:23 +0000
Subject: note 55469 added to language.references.arent
Groups: php.notes 
Request: Send a blank email to php-notes+get-93065@lists.php.net to get a copy of this message
References are not like pointers, period. In PHP, references are namespace aliases. In C/C++, pointers are memory addresses. While PHP sees references as having unique attributes, a C/C++ pointer is only a pointer because of its treatment. (That's why you can cast other data types to pointers if you're really brave, really stupid, or really smart.) What the manual says you can't do in PHP, <?php function foo(&$var) { $var =& $GLOBALS["baz"]; } foo($bar); ?>, you can do in C/C++ (though not how ansonyumo at email dot com said). Anysonyumo's second example would set baz to bar's value (99), not to bar itself. Since baz is an int, not an *int (a pointer to an int), it can only store a value. To really do what the PHP example seems to do, you would need to use code like this: -+-+- int bar = 99; void foo(int* a) { a = &bar; } int main() { int foobarbaz = 1; int *baz = &foobarbaz; foo(baz); printf("%d\n", *baz); // sends "99" to stdout *baz = 100; printf("%d\n", bar); // sends "99" to stdout return 0; } -+-+- ---- Manual Page -- http://www.php.net/manual/en/language.references.arent.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55469 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55469&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55469&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55469&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55469&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55469&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55469&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55469&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93065) next »