tags:

views:

67

answers:

3

Hi,

I'm new to object oriented php, so I'm not sure which classes do I need for programming. I want to write little program, to show information from the database. There is a web page with the form for user input above and some space for the database output. User can enter something and programm logic will query the database and make some output. This is all. The output will be formatted with css.

I want to write this in the best tradition of object oriented programming. I'm not sure with class organization.

Thanks

A: 

Here are a few hints:

Start with trying to connect to your MySQL database. You may want to create a class for handling this connection.

Head to php.net and look at the functions with the 'mysql' prefix to give you a handle on how to go about this.

You may also want to create a class for handling forms. It's really up to you. Start coding away and just have a go. Over time you'll get a better handle on things but the main thing is to get stuck into it.

Good luck!

Evernoob
are two classes enough? class for DB and class for handling user input and all output?
EugenA
it depends on how you want to abstract the different facets of what you're doing. Start with a couple or a few and you might realize that one can be split into two or several. It's all about grouping functionality logically.
Evernoob
+1  A: 

We really cant tell you exactly what classes you need. Its will vary depending on your design. I can tell you however that in my experience those new to OO programming typically don't create enough classes.

Its worth looking at SOLID to see what criteria your classes need to fulfil. And in particular the first principle (Single responsibility). This basically says that each class should be responsible for one piece of functionality, and that it should be the only class responsible for that functionality.

For example: you might create a class to connect to your database. This class should only handle database connections (it shouldn't also be responsible for serving pages or authentication or anything else), and all connections to the database should use this class.

Jack Ryan
+1 for SOLID. The UncleBob page actually seems to have better info than Wikipedia: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
Daniel Pryden
can i place the functions to query the database also in DB-class? And i should place the functions for page building into one class and functions for processing user input into another?
EugenA
Daniel Pryden, thanks for the link
EugenA
There are a number of patterns that can be used to create objects for your queries. The two things to google are the repository pattern and the query object pattern.
Jack Ryan
A: 

These classes can be: SQLConnection, SQLQuery, SQLQueryResultFormatter

FractalizeR