trq Posted July 29, 2008 Share Posted July 29, 2008 How could I call a function in a class? if($login->checkLoggedin){ $login->displayForm; // Call the function... somehow } A login (or authentication) class should have little or nothing at all to do with displaying forms, its only job should (and would) be to authenticate and login users. You need to try to understand them as ordinary objects designed to do one task and do it well. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602208 Share on other sites More sharing options...
maxudaskin Posted July 29, 2008 Author Share Posted July 29, 2008 I wanted the displayLogin to be a function so I don't have to write it multiple times. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602209 Share on other sites More sharing options...
DarkWater Posted July 29, 2008 Share Posted July 29, 2008 But the process of creating a form should not be delegated to a login object. You probably shouldn't even HAVE a login object. A User object that handles logins through a Database class and some other mapper classes would be best I think. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602228 Share on other sites More sharing options...
maxudaskin Posted July 29, 2008 Author Share Posted July 29, 2008 You lost me on that. Come over to the light side of the moon and explain that a little more please. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602231 Share on other sites More sharing options...
DarkWater Posted July 29, 2008 Share Posted July 29, 2008 But...but...Pink Floyd is on the dark side of the moon.... Anyway, you shouldn't have a login class that magically creates a form... Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602244 Share on other sites More sharing options...
maxudaskin Posted July 29, 2008 Author Share Posted July 29, 2008 Well, I suppose that living in the shadows of steel and concrete is cool, but still, come to the light, leave New York for a week, goto North Dakota and see how flat it is. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602247 Share on other sites More sharing options...
DarkWater Posted July 29, 2008 Share Posted July 29, 2008 Lol, North Dakota is so random. Why isn't there just one big "Dakota"? But I digresss. Back on topic here. When thinking about programming in OOP, you need to visualize each class as a real life object and how it would function. Let's say you had a User. His name is Bob. $bob = new User(); Now, let's say Bob wants to login. He goes to the magic login station and magically gives your script the proper values (for the sake of brevity). if ($bob->login($password)) { // Bob feels special now that he's logged in. } else { // Bob can't even remember his own password? What a failure. } Now, if he failed the login (which he shouldn't...I mean come on, it's a magic login station), he'd want the magic login station (a form) to come back so he can try again, right? Would it be his job to make a new form? Not at all. You could have a form helper class maybe: //Let's say you already set all the proper values and methods and whatnot for your form class //In the else block of that login code above: else { $form->render(); } See how it's delegated to another object? That's the goal of OOP, although brief. If you want, I'll write up some example classes to go along with this. Remember, this was a short example. If I wrote classes for it, they'd be like, more serious than this and actually show the principles in practice. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/page/2/#findComment-602254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.