Using multiple classes
Moderator: General Moderators
Using multiple classes
I am using ezsql (a class) to grab all my data from MySQL. I am also writing (my first) class (userinfo) to output some html populated with data grabbed by ezsql.
How would I go about using the ezsql class inside my userinfo class ? is it possible to use classes inside classes?
thanks.
Ed.
How would I go about using the ezsql class inside my userinfo class ? is it possible to use classes inside classes?
thanks.
Ed.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
you can "extend classes"
this will do just as it sounds, it will extend the class and add on to it. read the manual for better documentation on classes
Code: Select all
class A
{
//............
}
//other nonsence
class B extends A
{
//...........
}- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Re: Using multiple classes
you can use any and as many as you want, even nesting them one under another...ed209 wrote:I am using ezsql (a class) to grab all my data from MySQL. I am also writing (my first) class (userinfo) to output some html populated with data grabbed by ezsql.
How would I go about using the ezsql class inside my userinfo class ? is it possible to use classes inside classes?
thanks.
Ed.
Suppose you are in userinfo.php
Code: Select all
<?php
//include the class in our file...
include 'ezsql.cls.php';
class userInfo()
{
function userInfo()
{
//create an object of the class...
$ezsqlObj = new ezsql();
}
}- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
I don't think I'll extend the class as the functionality will be very different to the ezsql class. Basically, I use ezsql class to grab the data and my other class to display it so I think I'll just call one from inside the other. does that sound about right! (incase you hadn't guessed - I've never been trained in OOP!)
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Message Passing
There is a much better option. A very important part of OOP is the idea that individual objects acting as agents can communicate with one another as needed. The mechanism for this is message passing. One agent (object) tells another agent that it needs something. Here is a way to accomplish that.
In short, when you needed to deliver flowers to a girl, you decided that you were going to have a florist do it. You knew a florist named Angela so you called her up and asked her to do it (passed a message).
Once an object is aware of other objects and what they offer, it can pass messages to any one of them based on need.
Hope that helps,
Cheers
Code: Select all
class florist
{
function deliverFlowers($person)
{ echo 'Flowers Delivered!'; }
}
class desperateDude
{
var $florist;
function desperateDude(&$florist)
{ $this->florist=$florist; }
function getFlowersForGirl($girl)
{ $this->florist->deliverFlowers($girl); } /* <---- This is where you pass a message to the Angela, the florist object! */
}
$Angela = new florist;
$ed209 = new desperateDude($Angela);
$Zha_Zha = 'gorgeous_uber_rocket_girl';
if($ed209->seesGirl($Zha_Zha) && $ed209->mustHaveGirl==true)
{ $ed209->getFlowersForGirl($Zha_Zha); }Once an object is aware of other objects and what they offer, it can pass messages to any one of them based on need.
Hope that helps,
Cheers