ChadNomad Posted July 5, 2008 Share Posted July 5, 2008 Im new to OOP so my terminologies may be wrong. Is it pointless to use procedural scripting in OOP methods/functions? I'm trying to move over to OOP to make my code easier to update but I noticed it's basically the same... For example: class SystemInformation { // URI Variables var $page_id; var $reference_id; // Page Information function page_information() { $page_id = $_GET['id']; $reference_id = $_GET['ref']; // Category Page (i.e. central, development) if (isset($page_id) && !isset($reference_id)) { $category_information_query = mysql_query("SELECT * FROM categories WHERE category='".$page_id."'") or die (mysql_error()); $category_information = mysql_fetch_array($category_information_query); } // Specific Page (i.e. article, review) if (isset($page_id) && isset($reference_id)) { $section_information_query = mysql_query("SELECT * FROM indexation WHERE id='".$reference_id."'") or die (mysql_error()); $section_information = mysql_fetch_array($section_information_query); $category_information_query = mysql_query("SELECT * FROM categories WHERE category='".$section_information[category]."'") or die (mysql_error()); $category_information = mysql_fetch_array($category_information_query); } Is that completely pointless? I mean I can use classes for some good effect, such as a mysql wrapper, but should this style of scripting not be used? Link to comment https://forums.phpfreaks.com/topic/113368-procedural-scripting-in-oop-methods/ Share on other sites More sharing options...
gigas10 Posted July 6, 2008 Share Posted July 6, 2008 Do you understand the theory of OOP? It just makes it easier for code reuse, if you are going to use this function once, then theres not much of a point of having it as a function, but if you use it multiple times then yes, it would be ideal to leave it as a function. And btw you didn't close out your function Link to comment https://forums.phpfreaks.com/topic/113368-procedural-scripting-in-oop-methods/#findComment-582706 Share on other sites More sharing options...
smsiebe Posted July 11, 2008 Share Posted July 11, 2008 Do you understand the theory of OOP? It just makes it easier for code reuse The theory of OOP is not to just make it easier for code reuse. This is one of the benefits, certainly not the sole purpose behind it. OOP methodology came to be as an evolution in complex software design. Yes, one of the things they were try to manage was the massive amount of code going into these complex systems, but just as much so to manage the complexity through encapsulation. There were many other reasons and gains from OOP. Anyway, to the question: You're correct in thinking that your approach in wrapping procedural code doesn't really give you much, however, it is a good start. I see you mixing some parts of code that should typically be separated out when using an OOP approach. Try separating out all your DB calls to a DB class, each basic request being a method. Than, in your page_information class, initialize the DB object in your __constructor() method and call your requests from there. So your request for category information may look like: $category_information = $this->db->findCategoryInfo($pageId); Right now, focus on abstracting out these main parts and using it from your other objects. Get comfortable with the structure and the syntax, than you can tackle more OOP concepts. As the previous poster stated, code reuse is one of the benefits you'll get out of OOP - focus on this when initially refactoring your code when learning OOP. I would suggest getting a book or two on object oriented design, there are good ones out there that are specifically about OOP in PHP5. They can go into better detail about the advantages and methods to implement OOP than I can in a forum post =). Keep at it! Learn to code clean OOP code is a present to yourself and your fellow programmers having to come behind you! I assure you that the investment in learning the new methodology with the language you are already comfortable with will pay off in very little time. Regards, Steve Link to comment https://forums.phpfreaks.com/topic/113368-procedural-scripting-in-oop-methods/#findComment-588065 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.