OOP Opinions

Discussion in 'PHP' started by sandstorm140, Oct 31, 2008.

  1. #1
    Hey again everyone,
    I have recently been developing my skills and learning OOP php. I have a simple short session related class put together and I'm just wondering how good you think it is or what advice/tips/opinions you have.
    Thanks:

    
    class sessionCheck {
    	function sessionExist($i){
    		if (isset($_SESSION['username'])) {
    			$i = true;
    			return $i;
    		} else {
    			$i = false;
    			return $i;
    		}
    	}
    	
    	function goToPage($page){
    		if ($this->sessionExist($i) == true) {
    			require($page);
    		} else {
    			echo('Please Login');	
    		}
    	}
    }
    
    
    PHP:
    		
    $sessionCheck = new sessionCheck();
    $sessionCheck->goToPage('leftnav.php');
    
    PHP:
     
    sandstorm140, Oct 31, 2008 IP
  2. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why bother with the session_exist function when you could just do this:

    
        function goToPage($page){
            if (isset($_SESSION['username'])) {
                require($page);
            } else {
                echo('Please Login');   
            }
        }
    
    PHP:
    You don't need a function just to check for the existence of a session variable, it's a waste of code and will slow down everything in the long run if it gets called thousands of times.
     
    Christian Little, Oct 31, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Data organization?

    What if he needs to check the session elsewhere. If we use your ideology, we shouldn't be using classes in the first place.

    In OO programming, its better to have dependencies that are used more than once in their own function.

    Peace,
     
    Barti1987, Oct 31, 2008 IP
  4. THE-arabic-web

    THE-arabic-web Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thakssssssssssssssssssss
     
    THE-arabic-web, Oct 31, 2008 IP
  5. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Exactly what azizny said. I call upon that method several times. The way i'v learned, well been taught, was in OOP, write just about any step as a method.

    For example
    class bruthTeeth
    function open cabinet
    function reach for toothbrush

    That way it's much easier to add an extra step in there incase i'll need it later on. Of course, not all cases are like this, but apparently it's the better way to write code. Am I wrong?
     
    sandstorm140, Nov 1, 2008 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    Wouldn't it make more sense not to have a sessionCheck class but a whole user class? Considering the sessions are apart of the user authentification/information..
     
    crazyryan, Nov 1, 2008 IP
  7. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes crazyryan, it probably would. But for the time being, i'm only wondering about the structure of my code, is it pretty good for my first OO script or is there a much simpler way to accomplish the same thing using OOP?
     
    sandstorm140, Nov 1, 2008 IP