SharkBait Posted March 4, 2009 Share Posted March 4, 2009 Is it possible to have a class use another class? I have a MySQL class with some basic options: $MySQL->Connect() $MySQL->DoQuery() $MySQL->FetchArray() $MySQL->Disconnect() Now I am creating a new class to pull address information from a database. <?php class CompanyInfo { private $company; public $address; // array for like address, city, state, country etc. function __construct($company); $this->company = $company; } function fetchAddress() { // How do I use $MySQL->Connect(), $MySQL->DoQuery() etc here?? // Will store information from data in class to be used whenever } function getAddress() { // Pull address information on a needed basis } ?> Am I doing this right? Im getting tired of repeating a lot of my code Link to comment https://forums.phpfreaks.com/topic/147974-class-within-a-class/ Share on other sites More sharing options...
Maq Posted March 4, 2009 Share Posted March 4, 2009 I think what you're looking for is extends. There seems to be a lot of the same questions lately... Link to comment https://forums.phpfreaks.com/topic/147974-class-within-a-class/#findComment-776636 Share on other sites More sharing options...
SharkBait Posted March 4, 2009 Author Share Posted March 4, 2009 I thought extends worked on adding new functionality to existing classes? I'm looking to utilize 2 completely separate classes? Can I do something like this? <?php global $MySQL; $MySQL = new MySQL(); $MyLink = $MySQL->Connection('servers stuff here'); ... $CustomerInfo = new CustomerInfo('MyCompany', $MySQL, $MyLink); $CustomerInfo->fetchAddress(); // This uses $MySQL within the class to run queries echo $CustomerInfo->getAddress['city']; echo $CustomerInfo->getAddress['country']; ?> Would I "global $MySQL;" inside the CustomerInfo class too? Link to comment https://forums.phpfreaks.com/topic/147974-class-within-a-class/#findComment-776644 Share on other sites More sharing options...
Mchl Posted March 4, 2009 Share Posted March 4, 2009 Sure you can. Link to comment https://forums.phpfreaks.com/topic/147974-class-within-a-class/#findComment-776645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.