c0deur63 Posted November 5, 2014 Share Posted November 5, 2014 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 ! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 5, 2014 Share Posted November 5, 2014 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. Quote Link to comment Share on other sites More sharing options...
c0deur63 Posted November 5, 2014 Author Share Posted November 5, 2014 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 ? Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted November 5, 2014 Solution Share Posted November 5, 2014 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. Quote Link to comment Share on other sites More sharing options...
c0deur63 Posted November 5, 2014 Author Share Posted November 5, 2014 thanks ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.