/    Sign up×
Community /Pin to ProfileBookmark

How and when to use OOP?

From what I understand I should be using Object oriented programming when I’m building my site. I see the value of it, but I don’t really know how I should use it.

Let’s take one page of my site for example.
On this page the user fills out a lot of forms to later generate a business offer. The forms are information about the customer, what has been sold and some financing data.
This is saved in a mysql database.

So, should all these forms be generated by an object in php?

Should I have one object that retrieves the saved data from the database.

Another that generates the forms.

And another that retrieves post data and saves it in the database.

Am I understanding this correctly?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 04.2011 — There is no single correct answer. In part it depends on the size and scope of the project as well as personal preference and skill.

While a 100% object-oriented approach is feasible, I suspect you'll find the majority of OOPHP developers end up just directly writing their input forms as [X]HTML without the use of any PHP classes/objects for the actual [X]HTML, though they may well be using objects to get/organize the data used in the form. They're more likely to use objects for the form validation, and almost certainly for taking the validated form inputs and applying them to the database or performing other business logic on them.

Two things I might suggest are to read Matt Zandstra's [i]PHP 5 Objects, Patterns, and Practice[/i], and to play around a bit with the CodeIgniter framework to see one well-documented implementation of an object-oriented MVC pattern. (CI is by no means the only good framework choice, but I've found their documentation to be easy to read and reasonably complete. Check out their video tutorials for a quick taste.)

If you'd like to see an example of an object-oriented approach to web forms, see the PEAR::HTML_QuickForm2 package. (One of the primary goals of OOP should be re-use of existing code. ? )
Copy linkTweet thisAlerts:
@andx85authorMay 04.2011 — ...[/QUOTE]

Thanks a lot. I think buying a book on it probably is the way to go. I'll also be sure to check out CI.
×

Success!

Help @andx85 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...