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 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Referencing one class within another

    Hi

    I'm trying to get my head around this with classes. What I'm trying to do is use one class's functions within another class. Probably easier if I just show some code:
    PHP Code:
    class DB
    {
        var 
    $prefix;

        function 
    DB($host$db_user$db_pswd$db_name$db_prefix)
        {    
            
    // Connect
            
    $open mysql_connect($host$db_user$db_pswd)
                or die(
    'Unable to connect');
            
    $db mysql_select_db($db_name$open);
            
            
    // Set the prefix var
            
    $this->prefix $db_prefix;        
        }
    }

    class 
    Test
    {
        function 
    hello()
        {
            return 
    $db->prefix// Here lies the problem!
        
    }

    And my index file looks like this at the top:
    PHP Code:
    $db = new DB($host$db_user$db_pswd$db_name$db_prefix);
    $test = new Test();

    echo 
    $db->prefix// Works!
    echo $test->hello(); // Does not work! 
    Now, I can access the DB class just fine, everything works alright with the $db variable etc, but what I don't understand is how I can use DB within my new Test class. The error generated is a "Call to a member function on a non-object" on the row where I try to do this.

    Confused!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    2 ways, , actually 3 depends on your situation which you will use
    PHP Code:
    <?php
    class Test{
        function 
    test(&$db){
           
    $this->db=$db;
        }

        function 
    hello(){
            return 
    $this->db->prefix;
        }
    }  
    $db = new DB($host$db_user$db_pswd$db_name$db_prefix);
    $test = new Test($db);
    echo 
    $test->hello(); // should work  
    ?>

    OR you can aggregate the db class within test... though to avoid passing connection parameters around you are better off calling a static instance of your DB class...

    <?
    class Test{
        function 
    test(){
           
    $this->db=new static_instance_of_db();
        }

        function 
    hello(){
            return 
    $this->db->prefix;
        }
    }  
    ?>
    ?>
    or you can just make $db global

    google for `singleton` or `static instance` (easier in PHP5 than 4)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, that first method works great. And I understand it as well! Much appreciated.


  •  

    Posting Permissions

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