PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 21st, 2012, 04:21 AM
El Barto El Barto is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: South of England
Posts: 157 El Barto User rank is Corporal (100 - 500 Reputation Level)El Barto User rank is Corporal (100 - 500 Reputation Level)El Barto User rank is Corporal (100 - 500 Reputation Level)El Barto User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 12 h 34 m 2 sec
Reputation Power: 12
Send a message via MSN to El Barto
PHP-OOP - How to improve this class

Hello,

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?

JSfiddle

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,
El Barto.

Reply With Quote
  #2  
Old December 21st, 2012, 05:52 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,864 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 21 h 20 m 24 sec
Reputation Power: 813
Hi,

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
PHP Code:
 $obj->generic_method($specific_method, ...); 

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-OOP - How to improve this class

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap