Jump to content

Object Oriented Design - Am I Doing It Right?


jjacquay712

Recommended Posts

I'm new to OOP in PHP and am needing some guidance from experienced OO PHP programmers on how an object oriented php application should be set up. Right now I have 6 main classes:

 

  • HTML
  • Page
  • User
  • Session
  • Database
  • and Form

 

The HTML class is never instantiated, its just used to make HTML tags. The Page object is used to put information in certain places of the page. Say you wanted to change the title of the page, you would do this: $page->title = "This is a page title";. The User object would be used to hold all of the information about the person who is using the site. It would interface with the session object to figure out who exactly it is. The database object it just used to do SQL queries. The form object is used to generate and process forms. Here is a sample of all of the objects in a simple login script:

 

 

<?php
include("classes.lib.php");

$page = new Page;
$user = new User;

//Create new field objects.
$fields[] = new Formfield("username", "text", "Enter Username", true, "Username", true);
$fields[] = new Formfield("password", "password", "", true, "Password");
$fields[] = new Formfield("remember", "checkbox", "checked", true, "Remember me", true);
$fields[] = new Formfield("submit", "submit", "Submit"); 


//Create a new form and pass in the field objects.
$form = new form("Login", $fields, "post");

$page->body(HTML::div(HTML::h1("Welcome, Please Login"), "", "header"));

if ( !$user->is_loggedin ) {
    if ( $form->is_submitted() ) { //Checks of form is submitted.
        if ( $form->is_complete() ) { //Checks if all required fields are filled out.
            $form->sanitize_data();
            $page->body(HTML::pre(print_r($form->data, true))); //If the form is complete print out all of its data
        } else {
            $page->body(HTML::pre(print_r($form->errors, true))); //If not print the errors.
            $page->body($form->output());
        } 
    } else {
        $page->body($form->output()); //Prints the form if it hasn't been submitted.
    }
}

$page->title("Please Login");
$page->create_page();
?>

 

So the question... Am I doing it right? Do you have any suggestions on how I could improve this code? Any help is greatly appreciated.

 

 

                                      Thanks, John

I think you're on the right track.

 

In my opinion, I wouldn't parse simple HTML through PHP. The less PHP has to parse the faster your application will be.

 

When I'm creating a class I avoid using echoes. I stick to booleans , exceptions and returns.

 

Post your class.

Thanks for the suggestion. When I made these objects I was focusing mostly on  the ease of coding, not so much the efficiency. That's why I made the HTML class. If I wanted efficiency I would just code procedurally.

Yeah, I am of the mindset of keep HTML as HTML and PHP as PHP..... not to say creating a form class to assist in the creation / processing of the form is not a good thing, but to have php code creating blocks of div tags and h1 tags??

 

Thats going a little too far in my opinion.  Though the code is pretty :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.