Page 1 of 1
Using multiple classes
Posted: Thu Dec 08, 2005 8:48 am
by ed209
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.
Posted: Thu Dec 08, 2005 9:02 am
by shiznatix
you can "extend classes"
Code: Select all
class A
{
//............
}
//other nonsence
class B extends A
{
//...........
}
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
Re: Using multiple classes
Posted: Thu Dec 08, 2005 9:06 am
by n00b Saibot
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.
you can use any and as many as you want, even nesting them one under another...
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();
}
}
as simple as that....

Posted: Thu Dec 08, 2005 10:43 am
by ed209
Thanks for your help. both seem to work so I'll have to work out which on is easier to use.
cheers,
ed.
Posted: Thu Dec 08, 2005 10:46 am
by n00b Saibot
extending a class and using a class inside another class are two widely different things from OOP's point of view...
Posted: Thu Dec 08, 2005 10:55 am
by ed209
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!)
Posted: Thu Dec 08, 2005 11:02 am
by n00b Saibot
ed209 wrote: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!)
But you guessed the difference right, good work

Posted: Thu Dec 08, 2005 1:18 pm
by ed209
lucky guess
thanks for the help
Message Passing
Posted: Thu Dec 08, 2005 2:13 pm
by BDKR
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.
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); }
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

Posted: Thu Dec 08, 2005 2:24 pm
by ed209
thanks for the info, starting to get the hang of classes.
I wish it was that easy in real life!

Posted: Thu Dec 08, 2005 2:39 pm
by BDKR
ed209 wrote:
I wish it was that easy in real life!
OO or picking up Zha Zha? LOL...