Re: magic getter

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

 



If you want to block setting of public properties on your class, implement
the magic setter.

    class Foo {
        private $data = array();

        function __get($name) {
            return $this->data[$name];
        }

        function __set($name, $value) {
            if ($name != 'foo') {
                $this->data[$name] = $value;
            }
        }
    }

    $f = new Foo;
    $f->x = 5;
    echo $f->x;   // "5"
    $f->foo = 'bar';
    echo $f->foo;   // Notice: Undefined index: foo in php shell code on
line 1

Enjoy!
David

[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