The question sounds retrograde but I have about 50 variables being passed to a class. Basically an array of data I call 'filters' that is first passed to a prepFiltersMethod($filters). This prep filters method does some things sets some more filters needed in other methods for queries. It returns a larger subset of data of $filters after getting some additional things etc..
EDIT: This is HOW to control data type of question, I tried to clean up the question a bit, in short I'm asking how to handle a large input array into a class. if Junk data is passed along into it or deficient data, should we try to manage it. What I'm asking is if you have a big array coming into a class, would you take the extra step to write it into a new array to ensure it is the RIGHT data etc. is used for control purposes ? Weed out keys that shouldn't end up in it etc.? Say, take $filters array, pick out the keys we wanted and write it into a new array, and then use that ? In short I'm wondering if it would be helpful to use a builder to do this or how other people would handle. If we do nothing it works fine but may be a risk later down the road?
- Say we have a multi dimensional array called $filters.
- $filters is being passed to a class (contains the details of a user request).
- The $filters class goes through a prepare method that adds a few more items to this $filters array.
- Now that the filters are prepared and checked, it will be used to build query and return results from another method. $filters is passed to getInventory method that looks at the filters and builds out a query and returns data from the DB.
As it stands, $filters array is being passed around growing and shrinking as needed. Anything can be passed to the class with no gatekeeping, Should this be rectified? In other words, the method that first prepares the $filters array, should it copy the variables into a new array (e.g. build a new array) ?
Someone passes in :
$filters['x'] $filters['y'] $filters['z'] ... ... ...
fine enough those are all needed/usable, but what if they pass in
$filters['x'] $filters['a'] $filters['b'] ... ... ...
where 'a' and 'b' are not needed ?
Do we care about this ? should I employ something that ensures that the input data is more complete?
1) builder pattern? 2) create a new array and re-write them ? 3) Not worry about, let the other methods ignore data it doesn't need? 4) A standard class that can get and set 5) A global array in the class $this->filters (hate this idea) 6) Other ideas ?