Re: private $foo
| From: | Anthony Ettinger | Date: | Tue, 28 Mar 2006 20:23:43 +0000 |
| Subject: | Re: private $foo | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-232767@lists.php.net to get a copy of this message | ||
On 3/28/06, Jochem Maas <jochem@iamjochem.com> wrote:
> Anthony Ettinger wrote:
> > On 3/28/06, Jay Blanchard <jblanchard@pocket.com> wrote:
> >
> >>[snip]
> >>I see this all over the place, but I don't think it stores the variable
> >>in =
> >>$foo:
> >>
> >>class Foo {
> >>private $foo;
> >>
> >>public function __setFoo($arg)
> >>{
> >> $this->foo = $arg;
> >>}
> >>[/snip]
> >>
> >>I am always using 'foo' in conversation and finally said it enough that
> >>the CEO used it in a meeting the other day. Setter functions are cool.
>
> hmmm, who wants to write setters for 100's of properties? ...
> see below for an alternative ...
>
> >>
> >>
> >
> >
> > Nevermind, it does need private $foo; it works without it, but not if
> > you want to use a default from within the class.
> >
> > if you set private $foo = 'foo';
> >
> > print $f->__getFoo();
> > $f->__setFoo('bar');
> > print $f->__getFoo();
>
> <?php
>
> class Foo
> {
> private $foo = 'foo';
>
> function __get($k)
> {
> if (isset($this->{$k})) {
> return $this->{$k};
> }
>
> throw new Exception("non existing property!");
> }
>
> function __set($k, $v)
> {
> if (isset($this->{$k})) {
> $this->{$k} = $v;
> return;
> }
>
> throw new Exception("non existing property!");
> }
> }
>
> $f = new Foo;
> echo $f->foo,"\n";
> $f->foo = "bar";
> echo $f->foo,"\n";
> >
> > Yields:
> > foo
> > bar
....If you handle your properties all the same.
--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html