I need some Object Oriented PHP help

Discussion in 'PHP' started by absentx, Oct 20, 2011.

  1. #1
    Okay so getting my head around OOP and classes is something I need to do with PHP so I decided to just dive in today and start writing and see what I could come up with.

    I need to validate an array of dates for my reservation application so I figured I would start with that.

    
    
    class Reservation_validator {
    	
    	//we need to make sure all the dates are consecutive, we cannot have any gaps
    	public function dates_consecutive($dates_array)  {
    		
    		for ($i=0; $i<=count($dates_array); $i++){
    	      $unix_difference = strtotime($dates_array[$i+1])-strtotime($dates_array[$i]);
    	      $difference = $unix_difference/86400;
    			 if ($difference >1){
    				  $error_count++;
    			  }
    		
    	    }
    		
    		if ($error_count >=1){
    			return false;
    		}
    		else{
    			return true;
    		}
    
    	}
    	//we also need to make sure that the reservation doesn't exceed three weeks
    	public function three_weeks_max($dates_array){
    		
    		if (count($dates_array)>21){
    			return false;
    		}
    		else{
    			return true;
    		}
    		
    			
    	}
    		
    	
    	
    	
    }
    
    
    PHP:
    Then I use the class by doing what is below, but that is where things get foggy:
    
    
    $reservations_valid = new Reservation_validator;
    $result = $reservations_valid->dates_consecutive($session_dates);
    $result_two = $reservations_valid->three_weeks_max($session_dates);
    
    
    if ($result_two == true && $result == true){
    	echo 'your reservation is less than three weeks and is consecutive, you may proceed';
    //do the rest of the script and enter the reservation
    }
    else if ($result_two == false){
           echo 'your reservation is longer than three weeks, please fix it';
    //user must fix reservation
    }
    else if ($result == false){
           echo 'your reservation must be consecutive, if you wish to make two reservations use the system twice';
    //user must fix reservation
    }
    
    PHP:

    I guess I am a little confused about what we write in the class and how we access it in our view or controller, the way I am doing it above seems a bit cumbersome. Also at this point I am not sure why writing this class is any better than just writing two functions. I assume that is probably because I am not harnessing the full power yet. Any insight or feedback on this appreciated!
     
    absentx, Oct 20, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    1) You're adding more overhead by using a class than you are saving by using it. (Unless you're going to be using exactly the same code in many files, it's less code, and it runs faster, if you write functions. And if you're only using a function once, it's faster and smaller to just write the code where it's needed instead of making a function.)

    If you're writing a class you're going to be using all the time - say an email class - and you use it using a few lines of code each time, use a class. Otherwise the general rule is don't.

    2)
    
            
    if ($error_count >=1){
      return false;
    }
    else{
      return true;
    }
    
    PHP:
    is the same as
    
    return !$error_count >= 1;
    
    PHP:
    All excess code buys you is excess disk usage and excess !speed.
     
    Rukbat, Oct 21, 2011 IP
  3. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    I think you don't know benefits of OOP.
    Well OOP is more secure robust, flexible than ordinary programming style.
    suppose if you write a function in class 'a' and want to want extent it for class 'b', then there is no need you go to class 'a' and do some modification there. You can simply modify it with method overriding or overloading only for class 'b'. Thus you can use class 'a' function any where separately and can modify where ever you want.There are lot of benefits of OOP. I will suggest you be patient and give some time to OOP. You will find it easy as time will pass.
     
    webshore88, Oct 22, 2011 IP
  4. ustas19

    ustas19 Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try use array_filter with you date calc function to filter bad dates. OOP is good when it need.

    You can solve your problem in one string :)
     
    ustas19, Oct 24, 2011 IP
  5. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Excellent, I appreciate everyone's feedback
     
    absentx, Oct 24, 2011 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Your logic flow on your IF statements could also use some help -- Never check the same value twice... and booleans don't need == (though occasionally === is handy).

    
    if (!$result_two) {
    	echo 'your reservation is longer than three weeks, please fix it';
    } else if (!$result) {
    	echo 'your reservation must be consecutive, if you wish to make two reservations use the system twice';
    } else {
    	echo 'your reservation is less than three weeks and is consecutive, you may proceed';
    }
    
    Code (markup):
    Is two less comparison operations... this would also mean you don't need to store them in variables unless you need them later on.
     
    deathshadow, Oct 24, 2011 IP
  7. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well aware, learning by doing, thanks for the feedback.

    If I am not storing them in variables, how do I go about evaluating what the method has returned?
     
    absentx, Oct 25, 2011 IP
  8. ustas19

    ustas19 Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    replace

    $error_count++;

    with

    return false;
     
    ustas19, Nov 14, 2011 IP