Re: User class(es)

From: Date: Sun, 29 Apr 2001 00:17:41 +0000
Subject: Re: User class(es)
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-335@lists.php.net to get a copy of this message
At 05:03 2001-04-29 +0000, you wrote:
cool idea. Right now I'm going to work on the user portion. Is there anything you suggest (member vars or methods) that I should add so that it fits well into this session architecture?
As php yet haven't got correct object-handling I usually go with this notation: All private methods are prepended by a '_' (not private in Java's sense, subclasses can still use them). Static methods (also called class-methods) always begin with an uppercase, instance methods begin with lowercase. Thus: Private instance method: function _method() { }; Private static method: function _Method() { }; Public instance method: function method() { }; Public static method: function Method() { }; Now, the absense of an 'super' directive is quite bothersome. Therefore, in all methods I expect a subclass to overload I implement the actual method as private, and the extendable method as public. Such as: <code> class ClassA {
        function _method($var) {
                stuff....
        }
        function method($var) {
                return $this->_method($var)
        }
} </code> Now if I want to subclass this and extend the method I do like this: <code> class ClassB extends ClassA {
        function method($var) {
                new stuff.....
                return $this->_method($var)
        }
} </code> Of course, a better way would be to name all the implementation methods according to their class, such as: <code> class ClassA {
        function _ClassA_method($var) {
                stuff....
        }
        function method($var) {
                return $this->_ClassA_method($var)
        }
} class ClassB extends ClassA {
        function _ClassB_method($var) {
                stuff....
                return $this->_ClassA_method($var);
        }
} class ClassC extends ClassB {
        function method($var) {
                new stuff.....
                return $this->_ClassB_method($var)
        }
} </code> But I haven't really seen any need to do this, so I don't. Anyways, I haven't yet decided on the implementation and interface to Session and User objects. The Session-object should however contain methods for user login and user logoff, loggin (either in a database or in a file). User should contain methods for authorizing pages. Most php-projects do however use databases, and if you use PEAR you use a central DB-object. Now, you don't want to clutter the global scope, but a global $db var should be expected. I usually add a reference var ($db) to all my classes which is initialized in the constructor. Problems arise if you try to meld to projects together, such as adding a messageboard such as Phorum. Phorum uses a lot of global vars, sometimes screwing up my own. A solution is to declare one single global array for your project, such as $MY_PAGE. In this you put all vars which are not temporary in scope, such as $db and so on. Temporary vars can be global as it doesn't matter whether another project uses them or not. So, in your classes constructors: <code> class ClassA {
        var $db;
        function ClassA() {
                global $MY_PAGE;
                $this->db = &$MY_PAGE[$db];
        }
} </code>

« previous php.pear.dev (#335) next »