Using multiple classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Using multiple classes

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Re: Using multiple classes

Post 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.... :wink:
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

extending a class and using a class inside another class are two widely different things from OOP's point of view...
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post 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!)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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 :wink:
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

lucky guess :wink:


thanks for the help
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Message Passing

Post 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 :)
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

thanks for the info, starting to get the hang of classes.

I wish it was that easy in real life! :lol:
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

ed209 wrote: I wish it was that easy in real life! :lol:
OO or picking up Zha Zha? LOL...
Post Reply