I am relatively new to PHP having worked previously with Classic ASP. I am working on my first substantial application in PHP and decided to utilise classes. I therefore have an index page which does little more than call a method on a class and it does everything from there depending on what query strings have been passed.
I am also trying to maximise code re-use. As such I created a method which you pass details of a table and an array of column data and it will draw you a DataTable with Add, Edit and Delete buttons all of which work and will update the database. This mean that by writing only a handful of lines where needed, I save dozens if not hundreds of lines of code.
Doing this though has created an issue. So far, other than basic validation I have not needed to do much with the data other than update the table. However, what I am now moving on to requires more. In this case I am working on an annual leave system, and at this point can already see a number of extra's I will need to handle after New or Edit lines are returned, or a row is Deleted.
When updating Annual Leave Entitlement only one record can be current. So if the current record is current, update all the rest. This is determined by dates, and so sometimes may need to throw an error back to the user, other times update existing records.
If manually adding or deleting annual leave, you also need to update the total remaining for the year - in a couple of places.
I expect as I work through further there will be more examples that will need custom handling.
In hindsight I may have bitten off more than I can chew with my currently level of experience, but then I have always found this a good way to learn - even if it is painful sometimes!
I can see a couple of options, but not sure how I would go about implementing them. I hope the experts here know of some better ways and can point me in the right direction.
My ideas so far are:
Allow the name of a static method to be passed to the function. If this is present the section of the script to handle post data calls this method and ignores the default handling.
Pass an array of rules and processing instructions. Although this seems like it could become messy and complex very quickly.
Separate the code for drawing the table and handling the post data. Then I can call whatever method is suitable.
Allow the normal post handling to occur, then add additional steps outside of the method
I think the code is probably too long to post, but if you could help me with your recommendations and ideas I can hopefully convert this to finished code.