Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    calling an outer class from an inner class

    okay, so i have a class called $superclass

    inside superclass, i call and initialize another class called $tpl

    ($superclass->tpl)

    superclass contains a few different classes.

    Inside tpl i need to use another class that superclass is including. How do I do this?

  • #2
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    here is what I have tried so far:

    When i define tpl in superclass, I did it like this:

    Code:
    require_once(ROOT_PATH."includes/functions/tpl_parse.php");
    $this->tpl = new tpl_parse(); //Set Language class
    $this->tpl->superclass =& $superclass; //Allow superclass usage inside tpl class
    And in the tpl class, i tried:

    Code:
    $this->superclass->err->errno = 2;
    $this->superclass->err->throwerr();
    the two lines above work outside of the tpl class when called just starting with $superclass. How come I cant use those inside tpl?

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    the error im getting with the above code is:

    Fatal error: Call to undefined method stdClass::throwerr() in /mounted-storage/home45c/sub001/sc21473-GRUR/dev/forum/includes/functions/tpl_parse.php on line 52

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    If it really is a superclass/subclass relationship (ie, the subclass IS-A superclass), you should just extend the superclass:

    PHP Code:
    class Super
    {
        protected function 
    doSuperStuff() { echo 'asdf'; }
    }

    class 
    Sub extends Super
    {
        public function 
    Sub2()
        {
            
    $this->doSuperStuff();
        }
    }

    $test = new Sub(); // => asdf 
    If the superclass HAS-A subclass you could just pass it either to the initializer or to a setter method:

    PHP Code:
    class Sub
    {
        private 
    $super;

        public function 
    Sub(&$super null)
        {
            if (isset(
    $super)) $this->set_super($super);
        }

        public function 
    set_super(&$super)
        {
            
    $this->super $super;
            
    $super->doSuperStuff();
        }    
    }

    class 
    Super
    {
        private 
    $sub;
        public function 
    Super()
        {
            
    $this->sub = new Sub($this);
        }
        public function 
    doSuperStuff() { echo 'asdf'; }
    }

    $test = new Super();  // => asdf 
    PHP has historically had problems with circular references, I don't know if it's better now.

  • #5
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    why is that better than:

    PHP Code:
    class superclass
    {


        public 
    $vars = array();     //Superclass variable holder
        
    public $DB;                    //Superclass database holder
        
    public $err;                //Superclass error holder
        
    public $lang;                //Superclass Language holder

        //constructor
        
    function run()
        {
            
    //Initialize database connection
            
    require_once(ROOT_PATH."includes/functions/dconnect.php");
            
    //Add the DB class to the super
            
    $this->DB = new dconnect();
            
            
    // Include Error Function
            
    require_once(ROOT_PATH."includes/functions/err.php");
            
    $this->err = new err(); //Set error class
            
            // Include Language Function
            
    require_once(ROOT_PATH."includes/functions/languages.php");
            
    $this->lang = new lang(); //Set Language class
            
            
    require_once(ROOT_PATH."includes/functions/tpl_parse.php");
            
    $this->tpl = new tpl_parse(); //Set Language class
            
    $this->tpl->superclass =& $superclass//Allow superclass usage inside tpl class
        
    }

    What is different?

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You shouldn't write data directly into classes from outside because it breaks encapsulation.

    edit: thought it didn't work but it seems ok in my tests. above still applies though /\/\/\
    Last edited by ralph l mayo; 03-21-2007 at 07:16 AM.

  • #7
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    There has got to be a way to do what I need to do without drastically changing my classes around. Im way to deep to do that now.

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If a class is nested within another one and you're having issues with passing data then you should perhaps modularise your design a bit more. Typically nested classes are avoided most of the time and it's better to have the nested class as a completely separate class.

    Also, superclass isn't very descriptive, you should call it something with relates to the overall objective of the class perhaps?

  • #9
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you haven't defined $superclass as anything. You probably want to be using $this.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    $this->tpl->superclass =& $superclass;
    If you are using PHP 5+, please remove that &.

  • #11
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    hmm, i apparently needed to do:

    PHP Code:
    $this->tpl->superclass $this
    Thanks for the help everyone.


  •  

    Posting Permissions

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