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:
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.
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,
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?
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..
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?