Re: Strange class behavior
| From: | Teodor Cimpoesu | Date: | Wed, 19 Jul 2000 09:38:26 +0000 |
| Subject: | Re: Strange class behavior | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-7239@lists.php.net to get a copy of this message | ||
Hi Harsanyi!
On Wed, 19 Jul 2000, 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?
$id as a member is referenced using $this->id not $this->$id syntax.
> BTW, what does the "var" keyword do?
nothing, you can remove it if you like :) , really
> If I remove the "var" line I don't
> notice any difference....
well, remove the contructor too, make another class instace and
you'll see the diff.
-- teodor