Re: Strange class behavior
| From: | Steve Edberg | Date: | Wed, 19 Jul 2000 17:01:37 +0000 |
| Subject: | Re: Strange class behavior | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-7311@lists.php.net to get a copy of this message | ||
Hi -
Take a closer look at the examples in
http://www.php.net/manual/language.oop.php
Loose the second '$' in your class variable references - ie:
$this->id = $theId;
$this->name = $theName;
echo "Node id is: " . $n->id;
The way you had it originally, it worked sorta like variable variables; in your echo statement, for example, it first looked for the value of '$id' in your main program (_not_ in the class definition). If $id was set to 'gleep', your echo statement would be equivalent to
echo "Node id is: " . $n->gleep;
If perchance $id was set to 'id', the statement would actually have worked.
-steve
At 11:32 AM -0400 7/19/00, Harsanyi Alexandru wrote:
Hi, I'm relatively new to PHP and I got stuck at using classes and objects in PHP. Here is an example: --------------------- <html> <head></head> <body> <?php class node { var $id, $name; function node($theId, $theName) { $this->$id = $theId; $this->$name = $theName; } } $n = new node(1, "some_node"); echo "Node id is: " . $n->$id; ?> </body> </html> -------------------------- A class is declared with an $id and a $name members. The constructor initializes them. However, when I run the script, I get the following ansver: Node id is: some_node although I referenced $n->$id, which was initialized in the constructor to 1. What am I missing here? BTW, what does the "var" keyword do? If I remove the "var" line I don't notice any difference.... Best Regards, Alex.+--- unix and cats: -----------------------------------------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu |+----------------------- hard to befriend, but then friends for life! ---+