unxposed Posted January 29, 2009 Share Posted January 29, 2009 Hi guys, This is a two part question. I've just started to learn OOP, so am unsure on it's practical uses. 1.) Is this a good use of OOP, is it unneccesary, overkill, or just plain wrong? Why is this better than just creating standard variables from the queries? 2.) Is there anything else that could be optimised with my code in general, what would be best practice?? Thanks in advance! //* Get page details *// $page = '1'; //* Create a page class *// class pages { var $layout; var $layout_id; var $clean_url; var $title; var $description; var $keywords; var $language_id; var $language; } //* Create a new page *// $current_page = new pages(); //* Get basic page details *// $query = 'SELECT * FROM pages WHERE page_id = ("' . $page . '")'; $result = @mysql_query ($query); while ($row = mysql_fetch_assoc($result)) { $current_page->layout_id = $row['layout_id']; $current_page->clean_url = $row['clean_url']; $current_page->title = $row['title']; $current_page->description = $row['description']; $current_page->keywords = $row['keywords']; $current_page->language_id = $row['language_id']; } //* Get page layout type *// $query = 'SELECT name FROM page_layout WHERE layout_id = ("' . $current_page->layout_id . '")'; $result = @mysql_query ($query); while ($row = mysql_fetch_assoc($result)) { $current_page->layout = $row['name']; } //* Get page language type *// $query = 'SELECT code FROM languages WHERE language_id = ("' . $current_page->language_id . '")'; $result = @mysql_query ($query); while ($row = mysql_fetch_assoc($result)) { $current_page->language = $row['code']; } Quote Link to comment https://forums.phpfreaks.com/topic/142937-to-oop-or-not-to-oop/ Share on other sites More sharing options...
trq Posted January 29, 2009 Share Posted January 29, 2009 Simply using classes / objects does not necessarily mean your using OOP. Id suggest learning about design patterns, then you will appreciate OOP. As for your code. You are aware that it is written in the older php4 style syntax? Quote Link to comment https://forums.phpfreaks.com/topic/142937-to-oop-or-not-to-oop/#findComment-749439 Share on other sites More sharing options...
unxposed Posted January 29, 2009 Author Share Posted January 29, 2009 Ah okay, no i didn't realise... so changing var to public would sort this out? or are there more syntax errors? I've looked at some pattern tutorials, inc. some on this site, but can't quite get to grips with it. I'd find it easier if I could work with my own example and get some guidance on how and why to change things... so at the moment i have created a page blueprint, and then for each of my pages i'd create a new page object with all relevent attributes i need for each page - seeing as every page will have these attribute requirements I i didn't think there was any need in creating a child class... what is wrong with this approach? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142937-to-oop-or-not-to-oop/#findComment-749497 Share on other sites More sharing options...
RichardRotterdam Posted January 29, 2009 Share Posted January 29, 2009 The way you are using you "pages" class almost looks like a DAO(data access object). Maybe you will find articles about DAO and ORM(object relation mapper) of interest Quote Link to comment https://forums.phpfreaks.com/topic/142937-to-oop-or-not-to-oop/#findComment-749506 Share on other sites More sharing options...
unxposed Posted January 29, 2009 Author Share Posted January 29, 2009 Agh! two more acronyms i have never heard of. I'll look them up. If somebody could take the time to write a couple of sentances about why what i'm doing is fundamentally wrong it would be really appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142937-to-oop-or-not-to-oop/#findComment-749546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.