/    Sign up×
Community /Pin to ProfileBookmark

question about classes or advice on such

Hi

I have a script which handles customer records and license keys and a lot of other stuff. I have one file which contain the main function. This function will take about 20 arguments, where one of them is “action”. This variable will tell the main function what to do: create, update, select and other. Each value will actually launch another function, for example createCustomer, updateCustomer. In addition to these main functions there are a lot of “smaller” functions which does many different things. As I said the main function is in one file, and the rest are divided into several included files. All of the files has about 10-15000 lines of code all together.
I use the main function in several other scripts. I am now thinking about creating this in a better (?) way using classes/OO. Since I’ve actually never created any really “big” classes yet, I’m hoping somebody could help me/point me in the right direction.

I should only create one class which contains all functions needed? Like this:

[code=php]class customerClass {
public $Action;
public $Name;
public $Email;

public function executeCustomerClass(){
if ($this->Action == “create”){
$this->createCustomer();
}elseif ($this->Action == “update”){
$this->updateCustomer();
}else{
//unknown action
}
}

public function createCustomer(){
//Insert new customer record
}

public function updateCustomer(){
//Update customer record
}

public function doSomeCoolStuff(){
//some small function doing something useful
}
}[/code]

But then I would have to have everything in one file, and then some readability would be lost? Anybody got some advice here, or should I just buy a book? ?
Let me know if you need additional info to be able to help me….

Thank you very much
Lubox

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 08.2006 — It's a bit hard to give a definitive answer without knowing all the functional requirements. Basically, though, you want to think objectively: what real-world objects and processes do you want to model? Do they logically sub-divide into different sub-classes of objects which will have different data and/or processing requirements from their parent objects?

Then you may find you can have some of your current modularity by defining interfaces and/or parent classes, then more detailed classes which imlement the interfaces or extend the parent classes.

Also, there's really no need for the executeCustomerClass() in your example: you can just reference the desired public methods directly:
[code=php]
$customer = new customerClass();
$customer->createCustomer($data);
[/code]
Copy linkTweet thisAlerts:
@theRamonesNov 09.2006 — yes, you should buy a book, but i suggest a book that explain about OOP methods not how to implement OOP methods on program, because it's really different approach for OOP and structural programming.
Copy linkTweet thisAlerts:
@LuboxauthorNov 09.2006 — Ok, thank you for your answers.

I will of course set the processes down on paper before I start coding. What I'm hoping to achive is calling it something like this:

[code=php]
$customer = new customerClass();
$customer->Execute($data);
[/code]


(or similar)

$data is an array containing every variable the class will need, including the action-value, telling the class what to do. I do not really want the calling script to need to know what to do with the data, just collect it and send it to my class. I do not want it to check if it should create or update or whatever.

If we look at a really simple example of my requirements: The data I collect is action, name and email.

The action can contain "create" or "update", where the first creates a customer and the second updates it. In addition to this a lot of functions are needed during both create and update: some only for create, some only for update and some for both. How would you set up this class, keeping in mind that the create and update function may be really large, and should be in separate files. As I understand one class MUST be in one file. Can I include a file containing a class function inside the class somewhere maybe?

Should the customerClass extend another class? Sorry for asking a bit stupid here, but I hope I could just get a push in the right direction with this example.

Yes, I am going to get a book, but I'm just a little eager to get something down on paper ?
Copy linkTweet thisAlerts:
@NogDogNov 09.2006 — [Caveat: I am not an expert on OOP; I just know enough to probably give bad advice. ? ]

You might possibly consider difining a generic "database object" class that includes all the methods you need for database operations: connections, inserts/updates, select queries, etc. Then your Customer class could extend that class, thus inheriting all of its attributes and methods. If some day you need to change the database engine you're using or specifics for connecting to your database, you only need to change things once in that database object class.

If at some point you find you'd like to model different types of customers, then you might create additional classes which extend the basic Customer class, allowing you to change the parent methods and/or add new methods to represent the special requirements of the child class.
×

Success!

Help @Lubox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...