Well, I think what you need is multiple inhertiance that PHP doesn't support. Of course if we put some time on this we can find a way (it's always true about everything!), but why don't you pass your parent objects (those that you need everywhere) to your children objects? I know that it looks ugly somehow, but then I don't think that even inheritance can help ya. For example about this db object, you should instantiate it with some variables, like the db name, password and ... So, you need a constructor. But also in the child class that you use your db class (as a father), you need a constructor too, so, what's the point? If you write a constructor for the child class, then by instantiating the child object, the parent object doesn't get its variables and etc. I always pass my parent objects to the children and they're all ok!

PS: Currently I'm writing something similiar, I have a user class, which should access 3 different tables, means 3 different db objects! So, the only thing that I came up with was passing the db objects to the user class, that's very ugly, I know, but the best thing I got (yet!).

    HilmFreddy,
    I just responded to your previous post. But I think I see what you are trying to do a bit better now.

    First off, you need not use inheritence to gain access to another objects. You just need access to the object itself.

    class mySqlObject
    {
      /* Code to connect to a database */
      function connect ...
      /* Code to execute a query */
    
      function query($sql) ...
    }
    
    class phpBasic  /* NO extend */
    {
      function whatever()
      {
        global $db;
    
    $db->query('Select this from that');
      }
    }
    
    /* Make a global database access object */
    $db = new mySqlObject();
    $db->connect(...);
    
    /* Now create something that uses the database */
    $basic = new phpBasic();
    $basic->whatever();
    

    Get the idea?

      Hey guys, thank you so much for all the help, I'm going to sift through all of this and see what I can do but this really is a major help.

      Once again, thanks so much and I'll see how thiese things work. 😉

        Originally posted by ahundiak
        HilmFreddy,
        I just responded to your previous post. But I think I see what you are trying to do a bit better now.

        First off, you need not use inheritence to gain access to another objects. You just need access to the object itself.

        class mySqlObject
        {
          /* Code to connect to a database */
          function connect ...
          /* Code to execute a query */
        
          function query($sql) ...
        }
        
        class phpBasic  /* NO extend */
        {
          function whatever()
          {
            [color=red][b]global $db;[/b][/color]
        
        $db->query('Select this from that');
          }
        }
        
        /* Make a global database access object */
        $db = new mySqlObject();
        $db->connect(...);
        
        /* Now create something that uses the database */
        $basic = new phpBasic();
        $basic->whatever();
        

        Get the idea? [/B]

        That global thing is exactly what I meant (just in another form!).

        HiImFreddy: You're welcome dear! 🆒

          Using a global object will work, but there are some problems with that approach. To start with, there are reasons why globals are discouraged. Also, you need to know the name the global class has inside the other class.

          The best solution is to pass a reference to the data object to any classes you want to use it in. This is done with the reference operator (&). (In PHP5, this will be done automatically.)

          class mySqlObject
          { 
            function connect($host, $user, $pass)
            {
            	/* Code to connect to a database */
            }
          }
          
          class foo  
          { var $dbObj; // Pass a reference (&) to a mySqlObject to the constructor function foo(&$dbObj) // Constructor { $this->dbObj = $dbObj; } // Now this class can work directly on that object function dbConnect($host, $user, $pass) { $this->dbObj->connect($host, $user, $pass);
          } } /* Now you can do this */ $db = new mySqlObject(); $bar = new foo($db); $bar->dbConnect($host, $user, $pass);

          You can even access methods of the inner class like this:

          $bar->dbObj->connect($host, $user, $pass);

          The problem with that is that you have to know what the inner class is called (as in the global example).

          Finally, since the inner and outer db object are the same object (not a copy), you could call the db object's methods directly:

          $db->connect($host, $user, $pass);

          and the result will be accessible to $bar through the inner object.

            I don't disagree with dshearin's suggestion to store a reference to a global object inside of the data object.
            I often do this myself.

            However, keep in mind that this means that whatever code is creating your data object needs access to the global object and it needs to know that the object needs the global object.

            So, you basically end up shifting the need to access the global object to a different section of the code. And you make creating the object a bit more difficult. As with most things, it's a trade off.

              Hey guys, you all seriously rock. I just successfully coded a test app that does exactly what I was hoping for and will really make what I'm trying to do a WHOLE lot better. Another happy ending. 😉

              Once again guys, thank you so much!

                2 years later

                Hi, I know this is an old thread.

                But is this PHP4 compatible? It's two years old, so I'd assume it would be. If not, is there an equivalent method of doing this??

                  Well, PHP 5 wasn't around two years ago, so.......

                  Besides; a PHP 5 solution would have used a static property.

                    Thanks, I thought so.

                    Yet I asked because I got an error when trying their examples (I am trying to do a similar thing). Is there something in php.ini I need to enable, or what would solve this error:

                    Parse error: parse error, unexpected T_CLASS, expecting T_STRING in /home/thaspy/public_html/dev/uo/index.php on line 9

                    I am using the example in post #2.

                      Right, and did you go back to the manual to see how to extend classes? The error message you got is pretty accurate.

                        Well, I did that. Turns out their examples contained incorrect syntax, which I took to be accurate.

                        class b extends class a

                        should be

                        class b extends a

                        Thanks

                          Write a Reply...