I’m creating a kind of massive network for users to register and login. I want to try using classes, but I’ve never used them (expect some mysql-wrappers etc). Could you provide some tips and sample-structure for my project?
The idea is to simply have a index.php, which prints the whole page and does all the action. Index.php calls functions from classes inside other files.
I need:
- user-class for checking if logged in and retrieving user-info
- different kind of “page”-classes for functions needed in those pages
I’m not asking for full code, but just a start. I don’t know, how to use public functions or anything like that. How to wrap these classes to work together? So no functions, just the structure!
Martti Laine
Well for the first part, I’ll give you hint on using the index for everything. I use a switch statement that simply calls out everything, like so:
This code by itself does nothing, but it gives you a general idea of how you can switch between many different “pages” using just index. Adding a new page is as simple as adding another case to your switch statement.
As far as structure goes, I would really recommend you do some reading on MVC. It might seem complicated at first, but once you get the hang of it, it will save you a lot of time and trouble. Here are some good reads on it:
http://nemetral.net/2008/07/31/a-gentle-introduction-to-mvc-part-1/
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
Also, for general class structure, nothing beats php.net’s explanation of it:
Classes and Objects
I hope this helps.