tags:

views:

58

answers:

3

Hello

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

+1  A: 

I would recommend you look over some free resources. This will be more helpful than trying to explain everything in a post:

Zack
A: 

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:

<?php


switch($_REQUEST['mode']){

    case 'create':
        $ourhtml = $object->do_create();
    break;

    case 'read':
        $ourhtml = $object->do_read();
    break;

    case 'update':
        $ourhtml = $object->do_update();
    break;

    case 'delete':
        $ourhtml = $object->do_delete();
    break;

    default:

    $ourhtml = "<form action=\"index.php\" method=\"get\">
    <input type=\"text\" name=\"name\"> 
    <input type=\"hidden\" name=\"mode\" value=\"create\">
    <input type=\"submit\" value=\"create new\">
    </form>";

    <?php

    break;

    }

    echo $ourhtml;

?>

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.

Jeremy Morgan
Thanks, but I heard that using only one single class for whole site is not good. Is this true?
Martti Laine
Learned a lot from those links, thanks!
Martti Laine
You don't want to use a single class for the whole site, my code was just for illustration purposes. Generally my index file interacts with a single controller object, but there are many classes.
Jeremy Morgan
+2  A: 
Felix Kling
Yea, the idea is to get some experience on classes. That's why I'm trying to create it myself.
Martti Laine
@Martti Laine: It is still good the read the frameworks source code.
Felix Kling
That may be true. I'll check 'em.
Martti Laine