PHP Class Issues

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.

For example:
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.
LVL 11
John EastonDirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
If this is present the section of the script to handle post data calls this method and ignores the default handling.
Is this not a case for inheritance - deriving a class from the parent and extending or overriding the functionality.

Some things to think about in PHP

1. You can create dynamic function names - for instance - assume you have a bunch of different actions that can be performed
$action = '__' . $mydata->action;
if (method_exists($this, $action)) {

Open in new window

This allows for dynamic linking of data to actions in server code.

2. PHP provides a great means for creating generic code from form to DB in the form of the form array. By defining your input's on your form as array variables like data[firstname], data[lastname] it means all your data comes in in a single array that is immediately available in PHP and can be passed as is to validation routines and data storage routines which can then automatically process the array.

3. Separate rendering code from data processing code - a definite must in my book. In my DataTables class I pass it an iterator that is used to create the table. My Forms class takes a structure that is data and field information as well as errors that might have been generated.
When data is submitted it is passed to the action of  the controller which then passes it to the model for validation and storage - returns an array of errors if it fails - which is then fed back into the form.
Take this from someone who has been down this road many times - trying to make the uber-generic code ultimately creates more headaches than it solves - sometimes a bit more verbosity on the code makes for a simpler application - you need to find the balance.

Having said all that you might want to take a look at using an existing PHP framework - Laravel is creating quite a stir at the moment and is pretty comprehensive as a framework for handling all the nitty gritty bits. If you are serious about moving to PHP I would start there.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John EastonDirectorAuthor Commented:
Totally agree with separating rendering and data processing.  I am trying to learn to work in a more MVC type design, but coming from Classic ASP (and before knowing about classes) I am used to putting everything on one page.

I've not used inheritance yet.  At present I have a Page class which has most of the code for output - things like building the menu, output the header and footer etc.  This also has the methods for drawing my DataTable, however they are static methods.  Does this affect inheritance?

I.e. I currently call "page::drawEditableTable(a,b,c,d,e);" to output the table.  This function (like all my others) saves the HTML in a static variable which keeps getting appended to until it is eventually output to the browser with the header and footer.

Also, giving most of the code is in one method, would inheritance effectively overwrite everything in that method?

Finally, would dynamic function names mean I could write a custom POST function and pass the name in the method call, then in the function check if the function exists, if so use it, if not use the default?  If so this might work.
Ray PaseurCommented:
Does this [static methods] affect inheritance?
Yes.  Static methods cannot use $this.

Also, giving most of the code is in one method, would inheritance effectively overwrite everything in that method?
That is a code smell, often referred to as GodClass.  The general argument against large methods goes to the idea that application design should abide by the SOLID principles.  When a class or object has more than one reason to change, it violates the Single Responsibility Principle.  This is not inherently evil, but it can make your work much harder.  If you extend a class that has one large method, and you need to override the method, you have to override the entire method, and this can result in a lot of duplicated code.  It may be a better design to have several smaller classes with simpler methods.

Dynamic function names sound like multiple inheritance... It might be a better idea to look for a simpler solution set.

PHP has good documentation on its object model.  It's in the Language Reference:
John EastonDirectorAuthor Commented:
I think dynamic functions should work, although Ray is also right about my coding style still needing work!

Thanks both for your help.
Ray PaseurCommented:
I think dynamic functions should work...
They do work.  Anonymous functions work too.  There are lots of things that "work" but that alone is not reason enough to use them.  The most important part of computer programming is writing code that is clearly stated, obvious and easily understood by human beings.  The more variables a script has, the more likely one or more of them will be wrong.  This is one of the central advantages of OOP design.  With fewer variables and fewer paths through the software it becomes possible to write automated tests.  With automated tests it becomes possible to test every variable value, every program path, and every change.  The result is programming that can be proven to be error-free.  So it is axiomatic that we want to simplify our code, separate concerns, and minimize the use of variables.

Best of luck with your project, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.