Re: private $foo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/28/06, M. Sokolewicz <tularis@xxxxxxx> wrote:
> Anthony Ettinger wrote:
> > On 3/28/06, Joe Henry <jhenry@xxxxxxxxxxxxxxxxxxx> wrote:
> >
> >>On Tuesday 28 March 2006 1:12 pm, Jochem Maas wrote:
> >>
> >>><?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";
> >>
> >>Maybe I'm wrong, but I thought you couldn't use the  "$f->foo" to access
> >>private variables from outside a class?
> >
> >
> > I think he means:
> >
> > echo $f->__get('foo');
> > $f->__set('foo', 'bar');
> > echo $f->__get('foo');
> >
>
> no, he doesn't. http://www.php.net/manual/en/language.oop5.magic.php
> should explain it, however the part about __get() and __set() isn't
> quite finished yet (and thus missing). Rest assured though, Jochem
> showed a correct way of handling this "problem", though PHP had a bug
> relating to recursive lookups when using isset() in combination with
> __get().
>
>

private $foo; cannot be accessed directly outside the script.

print $f->foo; #fails

Fatal error: Cannot access private property Foo::$foo in
/x/home/username/docs/misc/php/client.php on line 11




--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux