SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    i must be losing my mind...the foundation of inheritance is crumbling

    i will start off with my class structure

    PHP Code:
    interface Viewable {
        
        function 
    set($name$value);
        function 
    display();
        
    }

    abstract class 
    ModuleController implements Viewable {
        
        private 
    $modules = array();
        private 
    $template;
        public  
    $canHaveModules;
        
        function 
    __construct(){
             
    $this->run();
        }

        function 
    addModule(Viewable $module){
            if (
    $this->canHaveModules){
                
    $this->modules[] = $module;
            }
        }
        
        abstract function 
    run();
        
        function 
    set($name$value){
            
    $this->template->set($name$value);
        }
        
        function 
    display(){
            return 
    $this->template->display();
        }
        
    }

    class 
    LeftMenuModule extends ModuleController {

        function 
    run(){
            
    //code to compile left menu
        
    }
        
        function 
    display(){
            return 
    $this->template->display();
        }
        

    now...you will notice that LeftMenuModule.display() is the exact same as its parent, so you would THINK that it's redundant unnecessary code. the only problem is if i DON'T have the display() method redefined, it will say the method doesn't exist! it does this with set() as well. here is the exact error: (edit: just to clarify, it only generates an error if i try to call the undefined method, it works fine if i never redefine display(), and i never call display() either.)

    Code:
    Fatal error: Call to a member function display() on a non-object in {ROOT}/classes/ModuleController.php on line 26
    is there some fundamental thing i'm missing? because i THOUGHT the whole point of using inheritance was so that you could use the same methods over without having to redefine the exact same code over again. does it have to do with the fact that my ModuleController is abstract? or maybe that my Viewable is an interface?

    basically, with all the required methods redefined, it runs fine...but i really don't think i should need to redefine them all, as that would be defeating the purpose of extending the previous class.

    thanks for your input,
    blayne

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this all the code? You're calling to $this->template, but it's never initialised. Besides, the variable is declared private. private variables aren't accessible to child classes -- use protected instead.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, this isn't all the code... where i have "code to compile left menu" is where everything is setup, including the declaration of $this->template. i just sorta figured it wouldn't be necessary to show since it wasn't related to how the classes related -- which i thought was the problem.

    but...you're right the problem the entire time was that $this->template was private. oh man do i feel dumb...i spent literally all day yesterday and today trying to figure this out, and i never even looked at the accessibility of the $template variable. i need to stop coding in the middle of the night, i know it's not healthy.

    thanks a ton, again.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I rarely use private. Per default, I either declare a property protected or public. I think it's a pretty good strategy; You don't really need the further isolation, unless you're writing library code (Eg. 3-party developers are extending your classes), so it isn't worth the confusion it adds.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's your take on getters and setters? i feel using getters and setters are just as bad as making the properties public...so usually if i need to use a instance variable outside of the class, i just make it public. it hasn't presented a problem yet...and i usually just write code for personal use i.e. not for open sources projects.

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think accessors (getters/setters) are bad in themselves, but they are often a symptom of trouble. Usually, if a class has lots of accessors, there are some other classes, which are too hardly coupled to it. Look if you can move the code, which uses the accessors into the class, which exposes its privates through the accessors.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •