Martti Laine
Martti Laine

Reputation: 12985

Tips about a good class-structure for website? (php)

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:

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

Upvotes: 2

Views: 1376

Answers (3)

Felix Kling
Felix Kling

Reputation: 816354

  1. Read about Object Oriented Programming in general.
  2. Read the manual about OOP in PHP

You really should get your head around OOP.
Afterwards read about some design patterns that are commonly used in web applications, e.g.

Have a look or even use the Zend Framework or any other framework. You can learn the most if you look at other sourcecode. But in order to understand the code, you have to understand OOP.


If it is not for learning purpose I wouldn't write such an application from scratch.
Use a framework that already implements the typical patterns and tools to help you (especially as you are not used to OOP, but nevertheless you have to learn OOP).

Upvotes: 3

Jeremy Morgan
Jeremy Morgan

Reputation: 3372

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.

Upvotes: 2

Zack Marrapese
Zack Marrapese

Reputation: 12080

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

Upvotes: 2

Related Questions