Jump to content

Using a class in another class.


SharkBait

Recommended Posts

Not sure if i have this right... but

 

I have a MySQL class that I want to use in a function of another class. Let's see if I can show it. Please correct me if I am wrong, I'm learning OOP ;)

 

<?php

class MySQL {

   function DoQuery($query, $link) {
      // Do Stuff
   }
}

class FunStuff {

    function DoMore() {
        $blah = $MySQL->DoQuery('blah', 'blah2');
        $query = $MySQL->FetchArray($blah);

     return $query;
    }
}

$MySQL = new MySQL();

$Fun = new FunStuff();

while($Fun->DoMore) {
   //Do more stuff
}

?>

 

What I am trying to do is be able to pass my $MySQL object into the $Fun class so I can use it to run queries. Now I don't know if its workable or if I am going about it wrong but I'd like to know :)

 

Thanks!

 

 

Link to comment
Share on other sites

I think it should go like this

 

<?php

class MySQL {

   function DoQuery($query, $link) {
      // Do Stuff
   }
}

class FunStuff {

    function DoMore(MySQL &$MySQL) {
        $blah = $MySQL->DoQuery('blah', 'blah2');
        $query = $MySQL->FetchArray($blah);

     return $query;
    }
}

$MySQL = new MySQL();

$Fun = new FunStuff();

while($Fun->DoMore($MySQL) {
   //Do more stuff
}

?>

Link to comment
Share on other sites

<?php
class MySQL
{
    function DoQuery($query, $link)
    {
        // Do Stuff
    }
}

class FunStuff
{
    private $_db;

    function __construct($db)
    {
        $this->_db = $db;
    }

    function DoMore(MySQL & $MySQL)
    {
        $blah = $this->_db->DoQuery('blah', 'blah2');
        $query = $this->_db->FetchArray($blah);
        return $query;
    }
}

$db = new mysql();
$fun = new FunStuff($db);
?>

 

Hope this helps

Link to comment
Share on other sites

You should probably use matthewhaworth's example if you are going to use the database object in the class (note that you should then not have the $MySQL argument in the method). If you on the other hand only need to use the database object in that specific method then you could do as ahowell did.

Link to comment
Share on other sites

Or you may want to extend the DB class with your FunStuff class...

class MySQL {
     // MySQL stuff

     function MySQL() {
           // Initiate this bad boy
     }

     function sql_query() {
           // Query somethin
     }
}

class FunStuff extends MySQL {
     
     function FunStuff() {
          // When this initializes you SHOULD have all your MySQL class functions available within this construct, although I've
          // encountered wierdness with this in the past. Try the MySQL class init function here if you're not getting an initialization
          MySQL();

          // If that doesn't work, try the :: operator for accessing class functions without an actual instance of that class
          MySQL::sql_query();

          // But all in all, if the init went well, you should be able to refer to this object, since it has inherited the MySQL classes' functionality
          $this->sql_query();
     }
}

 

Or, you may want the objects seperate because you find yourself needing both of them often enough in your scripts that you can pretty much count on one being there when the other is. In that case you can always refer to one within the other via its variable reference, obviously so long as that instance is actually defined before the call to it is made.

 

Basically, if I were you I'd fiddle around with the class functionality a bit, its fun stuff, definitely lends for some VERY scalable projects.

 

Anyway if this wasn't helpful sorry:) New on the board and just kinda shootin here and there trying to lend a hand ;)

Link to comment
Share on other sites

Or you may want to extend the DB class with your FunStuff class...

 

That would be a bad idea. extend is for extending the functionality of a class. That is not what you wish to do in this case so you should not use extend. You should rather create a new object and access it like e.g. $db->whatever();.

Link to comment
Share on other sites

this is how I'd do it...

 

<?php

class MySQL {

   function DoQuery($query, $link) {
      // Do Stuff
   }
}

class FunStuff {

    function DoMore() {
        $blah = $this->db->DoQuery('blah', 'blah2');
        $query = $this->db->FetchArray($blah);

     return $query;
    }
}

$MySQL = new MySQL();

$Fun = new FunStuff();
$Fun->db = $MySQL;

while($Fun->DoMore() {
   //Do more stuff
}

?>

Link to comment
Share on other sites

  • 2 weeks later...

To fix the way you were doing it before, use the global keyword to access global variables:

function DoMore() {
global $MySQL;
$blah = $MySQL->DoQuery('blah', 'blah2');
$query = $MySQL->FetchArray($blah);
return $query;
}

But if you want a different approach, I'd use the static style (MySQL::query()...) if it's only basic stuff you want.

If you're looking for more advanced stuff with your database, do something else. What I'm doing with Thacmus is basing all database classes off of DBI, which also contains static functions for my MySQL wrappers. (The stylesheets for the site are broken, so you might want to view the plain text version)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.