December 21st, 2012, 05:21 AM
How to improve this class
I've been looking more into how best to use OOP in relation to PHP as I don't think I'm making the best use of it currently and no matter how many sites I read it isn't really helping me improve my coding in any practical way. I thought if I gave an example of part of a project I'm working on and how I've done it someone might be able to give me some ideas on other methods of doing it and what the advantages would be.
I've put the code up on JSfiddle as I don't know of a better place to put it and adding it to this post would make it very long, the code itself won't run as I've taken out a lot of the 'inner workings' and replaced them with comments as I don't think seeing exactly how each method is implemented is directly related to this question. Also if I left it in there'd be a lot more code for people to go through.
Part of what I'm building is a CMS and the example I'm giving here is of the events part of the CMS which retrieves events, updates events & changes their status (live & not live).
I've structured it so there is a database_get method & a database_put method which gets results from and puts values into the database, these are the only two methods which scripts initialising this class use to interact with. They have a parameter to identify what needs to actually be done, for example in database_put they can create a new event, update and existing event, take an event offline, take an event offline & delete an event. Is this good practise restricting all interaction with the class to just two methods and having a variable passed it defining what needs to be done?
Getting events from the database is all in one method too, so if I was trying to retrieve all the events from a database or just the one it'd be handled by the one method and depending on if an ID is passed to it works out if it needs to retrieve them all or just a specific event. Is this a good idea or should I separate them out into more than one method? Or is there an even better method?
Also if anyone knows of any good reading material on the subject, online or books, I'd be glad to hear about them as I'd certainly like to be a lot better with my OOP.
Thanks all for any advice / tips in advance,
December 21st, 2012, 06:52 AM
well, there are no "OOP laws", so don't expect definite answers.
Personally, I try to keep my code as simple and straightforward as possible. I think too many people get carried away by all the fancy OOP features and end up with gigantic class hierarchies and methods calling methods calling methods calling methods, which makes their code almost unreadable.
In your case, I'd leave out the delegation from database_get and database_put to the private methods. What's the point of that? Wouldn't it be just as good or even better if people could call the methods getEvents etc. directly? I generally wouldn't use this
pattern, because it's just too obscure compared to explicit methods. For example, an IDE cannot tell you what this class is capable of doing. You have to actually look at the code and go through the "switch" statement.
You might wanna read up on the MVC pattern as a good way of structuring code for websites.
Comments on this post