Results 1 to 7 of 7

Thread: OOP subclass conditions

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default OOP subclass conditions

    I have read many times that good OOP practice is to avoid conditions such as
    Code:
    if($a>$b){
    	if($a1<$b1){
    		if($a2<$b2){
    			//code here
    		}
    	}
    }else if($b>$c){
    	if($b2>$c2){
    		if($b3>$c3){
    			//code here
    		}
    	}
    }
    and you should subclass when you have messy code like this. but i havent been able to find exactly how to do this. can anyone show an example of replacing conditions with subclass please
    Last edited by ggalan; 07-18-2011 at 01:04 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    well, the ideal is to abstract out the way you deal with the code, and thereby avoid deeply nested blocks like this. for example (paraphrased from a current project of mine):
    PHP Code:
    if(!empty($_GET['page'])){
       if(
    preg_match('/^[A-Za-z0-9]+$/',$_GET['page'])){
          
    $result mysql_query("SELECT * FROM `pages` WHERE `page`='{$_GET['page']}' LIMIT 1");
          if(
    mysql_num_rows($result) === 1){
             
    /* do stuff with results */
          
    }else{ /* error: page not found */ }
       }else{ 
    /* error: page request not valid */ }
    }else{ 
    /* error: no page requested */ 
    this gets difficult to deal with very quickly. It can be abstracted out something like this:
    PHP Code:
    class page{

    public function 
    __construct(){
       if(!empty(
    $_GET['page'])){ $this->checkPage($_GET['page']); }
       else{ 
    $this->noPageRequested(); }
    }

    public function 
    checkPage($page){
       if(
    preg_match('/^[A-Za-z0-9]+$/',$page)){ $this->findPage($page); }
       else{ 
    $this->invalidPageRequest($page); }
    }

    public function 
    findPage($page){
       
    $result mysql_query("SELECT * FROM `pages` WHERE `page`='{$_GET['page']}' LIMIT 1");
       if(
    mysql_num_rows($result) === 1){ $this->doStuffWithResults($result); }
       else{ 
    $this->pageNotFound($page); }
    }

    public function 
    doStuffWithResults($results){
      
    /* do stuff with results */
    }

    /* other functions, etc. */


    this is an over-simplified example, but i hope it shows how it can make your code far cleaner, more organized, and manageable.

  3. The Following User Says Thank You to traq For This Useful Post:

    ggalan (07-18-2011)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Generally, think about your programming as doing things, rather than running through lines of code. This also helps if you want to reuse anything later. It's similar to using basic functions to simplify code but more powerful.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    ggalan (07-18-2011)

  6. #4
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i see
    traq, so from your example invalidPageRequest and pageNotFound would be other methods in this class, correct?

  7. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    right (I just didn't write everything out, I think you get the idea)

  8. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    if the inheritance chain gets large and you have to change the top, initial method how do you deal with all the other methods? wouldnt this be similar to messy code?

  9. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    it depends on what sort of changes you need to make - if you need to completely re-write something, then it will be messy no matter what. But if you just need to change the logic/procedure, and you don't need to change the lower-level functions (as is often the case), then it's much cleaner.

    There's no actual "inheritance" (I don't think that's what you're really referring to) in the above example. Inheritance comes into play when you have classes that "extend" a parent class.

    Also, as Daniel said, one of the main advantages of OO code is that it is much more reusable - you don't have to repeat blocks of code that are used in several places in the script.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •