jjacquay712 Posted July 22, 2009 Share Posted July 22, 2009 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 Link to comment https://forums.phpfreaks.com/topic/167052-object-oriented-design-am-i-doing-it-right/ Share on other sites More sharing options...
9three Posted July 22, 2009 Share Posted July 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167052-object-oriented-design-am-i-doing-it-right/#findComment-880842 Share on other sites More sharing options...
jjacquay712 Posted July 22, 2009 Author Share Posted July 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167052-object-oriented-design-am-i-doing-it-right/#findComment-880845 Share on other sites More sharing options...
9three Posted July 22, 2009 Share Posted July 22, 2009 Pretty over efficiency? tsk tsk. Link to comment https://forums.phpfreaks.com/topic/167052-object-oriented-design-am-i-doing-it-right/#findComment-880848 Share on other sites More sharing options...
jjacquay712 Posted July 23, 2009 Author Share Posted July 23, 2009 I know, I should be shot for saying that. Link to comment https://forums.phpfreaks.com/topic/167052-object-oriented-design-am-i-doing-it-right/#findComment-880855 Share on other sites More sharing options...
chronister Posted July 23, 2009 Share Posted July 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/167052-object-oriented-design-am-i-doing-it-right/#findComment-880975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.