Jump to content

class strategy


c0deur63
Go to solution Solved by ginerjm,

Recommended Posts

Hello,

 

I'm starting using class and have a question for you guys : 

 

Let's consider a class "customer". I want to code a search tool. Where should I put my search function ? Within the customer class ? In another "search" class ? Other solution ?

 

Thanks for help !

Link to comment
Share on other sites

What does the customer class represent?  To me it would be something that manages the data about a customer - name, address, id - things like that.  You want a search function.  Is the search always on the same argument or is it a more general search ?  You might have a method in your class that always allows you to retrieve a specific customer given his 'key' field.  And you might have another method that searches on a more general sense that may use an associative array argument that you can then use to build a search query.  Yes - these two methods would be part your class.

Link to comment
Share on other sites

thx for the prompt answer.

 

In fact I'm planning to retreive basic info (name, adresse etc.) within the construct method. Is that sounds good  ?

Then if I've well understand your advice I should add a "search_customers($searchterms)" method that would return an array of the matching customer for example ?

Link to comment
Share on other sites

  • Solution

No.

 

I wouldn't build a search in the construct.  The construct should just create the object then you would use other methods to retrieve data or to add/insert data.  How could you do a search for a customer in the construct if you are creating that customer??  You could do a construct in the search method but not the reverse.

 

As for the alternate search method - the array would be an array of items to be used in the search - say 'city' and 'last name' - to build the query's where clause.  The results of this query would be either the resource that was returned or an array of customer objects.  Personally I think returning the query results would be better and then you let the caller handle it as it wishes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.