Jump to content

PHP Classes


maxudaskin

Recommended Posts

 

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

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.  :D

}

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.