Self-referencing variables revisited

From: Date: Tue, 28 Mar 2000 07:22:36 +0000
Subject: Self-referencing variables revisited
Groups: php.version4 
Request: Send a blank email to php-version4+get-12722@lists.php.net to get a copy of this message
Some time ago I proposed adding the converse of 'variable variables'. Something such as nameof($foo), which would all code to know the name of the object in question. My proposal was shot down by several folks whose opinions all reduced to 'just write "foo"'. Not having at hand a good example nor the time to make one, I deferred to all y'all doing the heavy lifting and dropped the subject. Now I have two examples. 1. I have a recursive function, pray() "print array" which recursively determines the type of an object and prints it. This is very handy for debugging. However, if I write pray($foo), I cannot print the name of the variable. I could rewrite the function to take a string instead, such as pray ("foo"), but this is counter to other usage in PHP - I cannot replace print($foo) transparently. I use this function extensively in debugging. It was invaluable in my learning the workings of PHPLib. 2. I cite the PHPLib documentation (documentation-3.html, 'How "serialize()" operates': > Should $t indicate an object, code is generated to create that object > (expression = new Classname;). Since one > cannot find out the name of the class of an object for arbitrary > objects in PHP, objects handled by serialize() must > have a slot named classname. The object handler will then enumerate > the contents of the objects slot > persistent_slots and call serialize() recursively for each of these > slots with the appropriate prefix. This points out a similar problem, using the PHPLib function $sess->serialize(). If the function 'nameof($foo)' existed, then the extra variable "Classname" would not be required in every class to be serialized. Of course, PHP4 now has a builtin serialize function, which no longer requires nameof(), only because the function now has access to the internal tables of PHP. This demonstrates the need is there for this functionality for PHP writers, as it was necessary to move this function into the core. 3. I just came up with this one, related to the last. If nameof() were a part of the language, then classes could have the ability to provide more useful error reporting, without explicit names in the code. Thus, one might have printf ("%s: Error %d - %s\n",nameof ($this), $this->errnum, $this->errstring) It appears to me that nameof() brings up the whole question of introspection... -- "Cyber is cyber, life is life." Gary E Bickford mailto:garyb@fxt.com FXT Corporation http://www.fxt.com/ tel:541-383-2749 mail:PO BOX 1808, SISTERS OR 97759 ship:66265 JERICHO ROAD, BEND OR 97701

« previous php.version4 (#12722) next »