Jump to content

Class within a Class?


SharkBait

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.