Re: Static properties

From: Date: Wed, 26 Apr 2006 20:59:56 +0000
Subject: Re: Static properties
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-22980@lists.php.net to get a copy of this message
Marcus Boerger wrote:
It is just some decision that was taken that the newer OO stuff should be a bit more strict.
I don't remember any such decision. I don't even remember a discussion about it. I'm aware of the efforts of some people to make PHP less PHP and more Java and thus we end up in a situation where: class foo{}; foo::$bar = 1; is a fatal error. This makes things that I really like about PHP impossible. By now I'm used to being not constrained by a static language and having such restrictions placed on new features makes no sense to me. I write code that does things like this: class Customer {
    function get($id)
    {
        $res = $DB->query("select * from customer where id=".((int)$id);
        while ($row = $DB->fetchRow($res)) {
            foreach ($row as $key = $value) {
                $this->{$key} = $value;
             }
        }
    }
} All nice and dynamic. Now, I thought why not make my site options work in a similar way but use class variables (a.k.a. static properties). So I made my options class class options {
    function load()
    {
        $res = $DB->query("select * from options");
        while ($row = $DB->fetchRow($res)) {
            self::${$row['KEY']} = $row['VALUE'];
        }
    }
} so I could for example througout my code use simple: if (options::$allow_anon) {
    ...
} without having to make an istance of the class. Simple and beutiful PHP way. All I needed to do to get a new site option variable was to insert "into options(key, value) values ('foo', 'bar')" and I would instantly have my options::$foo. Unfortunately I was faced with a fatal error about having to declare all static properties. I was afraid that this was not caused by any technical difficulty, but by desire to make PHP less PHP. I propose that we remove this restriction in PHP 5.2.0 and that we consider this attitude towards making PHP more strict and less dynamic in the future. Edin

« previous php.internals (#22980) next »