SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Classes within classes

    Hi Guys,

    Is it possible to access functions inside a class from within another class? If so, how?

    Thanks in advance.

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is, you call the functions like you would normally call a class function. For example:
    PHP Code:
    $DB->Query("SELECT * FROM users WHERE id = '$ID'"); 
    You have to assign the class a variable if it hasn't already been assigned, and then you just call the function using the above method ($ClassVar->Function(Paramaters);).
    Kayzio - We don't hesitate, we accelerate.

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but do you have to pass the class variable to the other class? Otherwise I just get the following error:

    PHP Code:
    Fatal errorCall to a member function on a non-object in 

  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)
    Quote Originally Posted by Zaggs View Post
    Yes, but do you have to pass the class variable to the other class?
    A class variable is called an object. I'm not trying to be Pedantic, but you really need to distinguish between a class and an object.

    Oh, and to answer your question -- Yes, you do. You would normally pass such dependencies in the constructor, so you only have to do it once.

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    A class variable is called an object. I'm not trying to be Pedantic, but you really need to distinguish between a class and an object.

    Oh, and to answer your question -- Yes, you do. You would normally pass such dependencies in the constructor, so you only have to do it once.
    Okay, let me get this straight..Lets say I have 2 classes:
    PHP Code:
    class One {
                
                function 
    register() {
                    
    // cose here
                
    }
                
            }
            
            class 
    Two {
                
                function 
    random() {
                    
    // code here
                
    }
            
            } 
    From within 'class Two', how would I call the register() function inside 'class One'.

    Thanks in advance.

  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)
    You wouldn't call the class' function directly; Instead, you would create an instance of the class (called an object), and call the function on that. For example:
    PHP Code:
    class One
    {
      function 
    register() {
        echo 
    "One.register called";
      }
    }
    class 
    Two
    {
      function 
    random() {
        
    $one = new One();
        
    $one->register();
      }
    }
    // to use the code:
    $two = new Two();
    $two->random(); 
    This is a pretty lame example -- In reality, you probably wouldn't create the object just before using it. Instead, you might use the constructor:
    PHP Code:
    class One
    {
      function 
    register() {
        echo 
    "One.register called";
      }
    }
    class 
    Two
    {
      
    /**
        * This is a constructor. It's a special function, which is automatically invoked, when you crate a new instance of the class.
        */
      
    function Two() {
        
    $this->one = new One();
      }
      function 
    random() {
        
    $this->one->register();
      }
    }
    // to use the code:
    $two = new Two();
    $two->random(); 

  7. #7
    SitePoint Addict chestertondevelopment's Avatar
    Join Date
    Dec 2005
    Location
    Essex, UK
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class One 
        function 
    register() { 
            
    // cose here 
        
    }
    }
    class 
    Two {
        var 
    $one;
     
        function 
    Two() {
            
    $this->one = new One;
        }
        function 
    random() { 
            
    // code here 
        

    'Two' class creates a new instance (an object) of the 'One' class when it's initiated and stores it as a normal variable. Example usage:
    PHP Code:
    $two = new Two;
    $two->one->register(); 

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    NL, Rotterdam
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Above examples cover quite a lot of the possibilities. In some scenario's you might want to pass an instance to an object instead of creating a new one from inside the object.

    PHP Code:
    class SomeClass
    {
      var 
    $objDb;

      function 
    SomeClass(& $objDb)
      {
        
    $this->objDb $objDb;
      }

      function 
    get_stuff()
      {
        return 
    $this->objDb->query_for_stuff();
      }
    }

    $objSomeObject = new SomeClass($objDb); 


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
  •