Jump to content

Recommended Posts

Hello everybdy,

 

Dealing with OOP last couple of days, I learned a lot. But I guess, I'm still missing the OOP concept and the logic behind it. Simply, I can't decide to use classes or not.

 

To make my understanding clear on OOP I would be greateful if you can help me on my example below.

 

For example, we need authentication for a website.

I defined AUTH_MODE variable in config file. It can have 3 values. "None","Simple" and "User". None is for websites we don't need private pages. Simple is for admin pages, where there is only one username and password stored in a config file. And User is for the systems there exist user registration with db interaction.

 

#1 Up to now, is everything ok? What I want to ask here is, do I need seperate class files for this three modes?

 

Let me continue with my example. I'm designing the User class now. It will have lots of methods like, login,confirm,lost_password,change_details..

And also there must be connection with my DB and Session Class..

 

#2 Should I put all the methods for User in the same class file or should I somehow split it into new files?

#3 And finally, whats the best way to include DB and Session classes? I mean is there any way other than instantiating my DB class in Auth class?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/88696-simple-oop-help/
Share on other sites

1) You don't have to, but you could have an abstract class called Login and then have some sub-classes called Login_None, Login_Simple and Login_User.

2) I'd say one class per file.

3) You could use a registry, a singleton or you could pass the object as an argument to the method.

Link to comment
https://forums.phpfreaks.com/topic/88696-simple-oop-help/#findComment-454310
Share on other sites

  • 2 weeks later...

par example:

<?php
/**
* User class.
*
*/
class user {

private $username;
private $password;

public function __construct($username,$password){
	$this->username = $username;
	$this->password = $password;
}

public function getUsername(){
	return $this->username;
}

public function getPassword(){
	return $this->password;
}

}

/**
* Abstract login class.
*
*/
abstract class login {

abstract public function auth(user $user);

}

/**
* Does the lookup in the file.
*
*/
class login_simple extends login {

public function auth(user $user){
	// open file
	if($user->getUsername() == $file['username'] && $user->getPassword() == $file['password']){
		return true;
	}
	return false;
}

}

/**
* Does the lookup in the database.
*
*/
class login_user extends login {

public function auth(user $user){
	// connect to database
	$db->query("SELECT id FROM users WHERE username='{$user->getUsername()}' AND password = '{$user->getPassword()}'");
	if($db->numRows() === 1){
		return true;
	}
	return false;
}

}

/**
* Does NO lookup, always authenticates.
*
*/
class login_none extends login {

public function auth(user $user){
	// Always auths.
	return true;
}

}


// Test run.
$user = new user("joe","pass");
$login = new login_simple();

// Authenticated.
if($login->auth($user)){
// do something here;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/88696-simple-oop-help/#findComment-463937
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.