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