Re: Scope of variable in Class
| From: | Lars Torben Wilson | Date: | Tue, 07 Nov 2000 03:37:38 +0000 |
| Subject: | Re: Scope of variable in Class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24047@lists.php.net to get a copy of this message | ||
Reuben D Budiardja writes:
> Suppose I have a class:
>
> class Cart {
> var $items;
>
> function add_item ($num) {
> $this->items = $num;
> }
> }
>
> I can access (ie. print) $items by doing:
>
> $myCart = new Cart;
> echo $myCart->items;
>
> My question: is there any way so that someone can NOT access $items? in
> order words, make $items a PRIVATE variable for the class only?
>
> Thanks
> Reuben D. Budiardja
No. You could always just prefix 'private' variables with an
underscore or something and tell users that anyone using '->_' unless
it immediately follows a '$this' will be shot, but that's about it.
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+