LITTLEBLACKDOG.COM Forum Index LITTLEBLACKDOG.COM

 
LWD LWD   FAQ FAQ   Memberlist Memberlist   Usergroups Usergroups   Active Topics Active Topics   Register Register  
  Profile Profile   Log in to check your private messages Log in to check your private messages   Log in Log in  
  Who is Online Who is Online   Image Gallery Image Gallery   Chat Chat   Search Search  
  LWDGear       LBDGear  

View next topic
View previous topic
Post new topic     Reply to topic   LITTLEBLACKDOG.COM Forum Index » Code Warriors
Author Message
fathertyme
Site Admin
Site Admin


Joined: 30 Jun 2001
Posts: 6213
Location: The American Colonies

Post Posted: Mon Mar 24, 2003 9:48 am   Post subject: PHP Object Oriented Programming Question Reply with quote Back to top  

OK... SO I'm looking at recoding the seti-parse script... I need to make it easier to understand, so that if there's a problem, I can readily identify what is wrong.

So I've been thinking of going OOP

but I've got ALLOT of data that gets run every tyme

so my question is...

what would be the best way to set up the class?

(I really wanna try and avoid array's...)

example... is this valid:

class Teamname {
var $teamwu;
var $members;
var $teamplacing;

class Position {
var $memname;
var $WU;
}

function add_member($pos) {
$str = "pos" . $pos;
this->$str = new Position; // maybe this->pos$pos = new Position;
}
}

of course that means that if there are 1000 members, you would have 1000 position class definitions... not to mention, I don't know if that whole $str="pos" . $pos; thing will work

anyways, if anyones got any ideas, or a butter way to structure it *(even if its just a feeling... eg: couldn't you do this?)* then I'd be more than happy to hear it

thanx

_________________
LWD web-cams: http://lwdcam.codecoma.com/?lwdcam
----

---
[9:08pm][09/16/2005]«+ flip » college...what is that
[9:08pm][09/16/2005]«+ Aff » apparently a place where you find rum
---
I used to live in my own little world, but they didn't like me there either.

You see dead people? I'm a software engineer, I don't see anybody!
---
My Amazon Wishlist
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
random
Moderator
Moderator


Joined: 30 Oct 2000
Posts: 3382
Location: Left Field

Post Posted: Sun Mar 30, 2003 6:25 pm   Post subject: Reply with quote Back to top  

Your brings up some questions.

bear with me im very new to OO and i am trying to understand.

It looks like you are nesting the class Position inside the class Teamname.

Is there a reason for that?
Can't you just delare everthing in the teamname class?

BTW why do you want to avoid array's

Also, you might have already tried it but.
if this...
$str = "pos" . $pos;
doesn't work.

this should.
$str = "pos".$pos."";

_________________
Democracy substitutes election by the incompetent many for appointment by the corrupt few. -George Bernard Shaw
Code: Select all
function video() {
die("radio star");
}
View user's profile Send private message Send e-mail AIM Address ICQ Number
fathertyme
Site Admin
Site Admin


Joined: 30 Jun 2001
Posts: 6213
Location: The American Colonies

Post Posted: Mon Mar 31, 2003 12:30 am   Post subject: Reply with quote Back to top  

actually, the reasoning is thus:

each team has certain characteristics.
each team also consists of members
each member has certain characteristics

so I'd like to create a team class where each member is created of class user

I haven't really started playing with this yet, so its all experimental

I might start by creating class user
and then creating class team, and using pointers

eg:

$lwd=new Team;
$(&[$lwd->$member[1]])=new user;

or some such crap like that... not really sure, gotta read more about php pointers and crap like that before I really start working on it

_________________
LWD web-cams: http://lwdcam.codecoma.com/?lwdcam
----

---
[9:08pm][09/16/2005]«+ flip » college...what is that
[9:08pm][09/16/2005]«+ Aff » apparently a place where you find rum
---
I used to live in my own little world, but they didn't like me there either.

You see dead people? I'm a software engineer, I don't see anybody!
---
My Amazon Wishlist
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
CMTG
Leg Humper
Leg Humper


Joined: 23 Feb 2002
Posts: 4963
Location: On average, Cheltenham.

Post Posted: Tue Apr 08, 2003 8:33 am   Post subject: Reply with quote Back to top  

I like OOP, though I have no php skills. C++/OGL are where my specialisations lie.

I agree with having the team class made up of instances of user classes. The way I would do it is to have a dynamically allocated array of user classes in the team class.

Code example now: (C++ only, sorry, shouldn't be too hard to interpret though.)

Code: Select all


//user class definition

class Cuser
{
private:
    string strName;
    int iWU;
    //other private data go here

public:
    Cuser(){};    //constructor
    ~Cuser(){};   //destructor
    int GetWU(){return iWU;};  //accessor function
    void SetWU(int iNewWU){iWU = iNewWU;};   //mutator function
    //other functions go here
};

//team class definition

class Cteam
{
private:
    string strName;
    Cuser* pUsers;   //pointer to a Cuser
    //other private data go here

public:
    Cteam(){};    //constructor
    ~Cteam(){};   //destructor
    void SetNumUsers(int iNum){
        pUsers = new Cuser[iNum];};    //<---see here 1
    void DeleteUsers(){
        delete [] pUsers;};            //<---see here 2
    //other functions go here
}


Now for the explanation:

The 'Cuser' class contains data members that describe a single user (attributes) and function members that allow you to manipulate those attributes (accessors/mutators). Simple enough.

The 'Cteam' class contains the same thing, only it includes as one of it's attributes; a pointer to a Cuser class.

The first mutator in the Cteam class, called 'SetNumUsers', marked by the "//<---see here 1" comment, sets the pointer to the address of the first item in an array of iNum Cuser classes that have been created and have had memory allocated.

The second mutator, labeled "//<--see here 2", deletes the array from from memory, the pointer now becomes invalid.

(As a point of interest, 'new' is a C++ memory allocation keyword, as is 'delete'.)

Using this method, you can create a team with a dynamic amount of users every time a new instance of Cteam is created and access the users through the pUsers pointer and an offset, ie:

Code: Select all

//from within the Cteam class
pUsers[4].GetWU();
pUsers[0].SetWU(10);

//from outside the scope of Cteam
Cteam ateam;   //an instance of Cteam
ateam.pUsers[4].GetWU();   //generates an error
                           //because of the private accessibility of pUsers


I hope I've helped, I may not have explained that all too well so if you have any questions about the above or object-orientation in general, please ask.

/* Edit - Seems I can't write English... */

_________________
Pie. I wish I could
constrain my hungry greed but...
Sadly, defeated.


Charlene's Law: There's no such thing as can't.
Charlene's Corollary: Unless it's followed by be arsed.
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic     Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
phpBB SEO
All times are GMT - 8 Hours

Help us keep advertisements off this site. Donate today!