Re: declaring variables in class definitions
| From: | Max A. Derkachev | Date: | Wed, 10 Jan 2001 09:05:37 +0000 |
| Subject: | Re: declaring variables in class definitions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-33635@lists.php.net to get a copy of this message | ||
Hello sam1600,
Wednesday, January 10, 2001, 6:42:58 AM, you wrote:
sic> What in the world is reason for declaring the following in the class?:
sic> var $somevar;
sic> I see no reason, and no differences if I don't declare: var $somevar;
Classes are intended to be structured data storage. You should define
a structure of the class before using the class.
You can set the value of the $somevar anytime later, when using an
object of class a.
$d->somevar = true;
and the next call to $d->b() will print nothing.
But you won't be able to assign a value to $somevar, if it is not
declared in the class.
sic> Maybe a better question is what will break if I do not declare
sic> my vars in my class definition?
the method b() of the class a will break if you don't declare $somevar
in the class definition. In the string "if (!isset($this->somevar))"
it would throw an error saying that you have no variable named 'somevar' in
you class.
--
Best regards,
Max A. Derkachev mailto:kot@books.ru
Symbol-Plus Publishing Ltd.
phone: +7 (812) 324-53-53
http://www.Books.Ru -- All Books of Russia