SharkBait Posted August 29, 2007 Share Posted August 29, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/ Share on other sites More sharing options...
ahowell Posted August 29, 2007 Share Posted August 29, 2007 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-337306 Share on other sites More sharing options...
matthewhaworth Posted August 29, 2007 Share Posted August 29, 2007 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-337321 Share on other sites More sharing options...
SharkBait Posted August 30, 2007 Author Share Posted August 30, 2007 Ok I see how the two above are different but why would I do it one way and not the other? Or are both valid? Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-337949 Share on other sites More sharing options...
Daniel0 Posted August 30, 2007 Share Posted August 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-337958 Share on other sites More sharing options...
fireMind Posted September 3, 2007 Share Posted September 3, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-340600 Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 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();. Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-340639 Share on other sites More sharing options...
ToonMariner Posted September 4, 2007 Share Posted September 4, 2007 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-341189 Share on other sites More sharing options...
deadimp Posted September 16, 2007 Share Posted September 16, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-349754 Share on other sites More sharing options...
Jenk Posted September 18, 2007 Share Posted September 18, 2007 Don't use the global keyword. Depending on the frequency of use, either pass the instantiated mysql class object to the constructor, or to an injector later. Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-350370 Share on other sites More sharing options...
ReDucTor Posted September 20, 2007 Share Posted September 20, 2007 class Mysql { static function getInstance() { return mysql_connect(); } } class SomeClass { private $connection; function __construct() { $this->connection = Mysql::getInstance(); } } Static funcs ftw! Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-351540 Share on other sites More sharing options...
Jenk Posted September 20, 2007 Share Posted September 20, 2007 Static funcs ftl! What if you want a different connection, with different parameters? Quote Link to comment https://forums.phpfreaks.com/topic/67235-using-a-class-in-another-class/#findComment-351596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.