Jump to content

Learning OOP Programming


gaza165

Recommended Posts

I have made a simple bit of OOP code which i was wondering if you guys could just check over, just to see if i have got the jist of OOP Programming....

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html;
	charset=iso-8859-1" />
	<title>OOP in PHP</title>
</head>
<body>

	<form action="process.php" method="POST">
        <table width="300" border="0">
          <tr> 
            <td width="77" class="label">Username: </td>
            <td width="150"><input class="text" type="text" name="user" maxlength="30"/></td>
            <td width="144" class="error"> 
             </td>
          </tr>
          <tr> 
            <td width="77" class="label">Password:</td>
            <td width="150"><input type="password" class="text" name="pass" maxlength="30"/></td>
            <td width="144" class="error"> 
              </td>
          </tr>
          <tr> 
            <td> 
              <input type="hidden" name="action" value="login" />
              </td>
            <td> 
              <input name="subit" type="submit" class="loginbutton" value="Login" />
              </td>
            <td width="144"></td>
          </tr>
	  <tr><td colspan="3" class="error"><? if(isset($_SESSION['msg'])) { echo $_SESSION['msg']; unset($_SESSION['msg']);} ?></td></tr>
        </table>
      </form>
 </body>
</html>

 

class_lib.php

<?php 		
class User {		

	function __construct($username,$password) {		
		$this->username = $username;		
		$this->password = $password;					
	}			
	function get_username() {
		return $this->username;
	}
	function get_password() {
		return $this->password;
	}
	function check_Login() {		
		if($this->username == 'gaza165' && $this->password == 'tester') {
			header("Location: http://www.google.com");
		} else {
			echo "not logged in";
		}
	}
}


?>

 

process.php

<?php

include("class_lib.php");
include("dbconnect.php");

$username = $_POST['user'];
$password = $_POST['pass'];

$user = new User($username,$password);

echo $user->check_Login();

?>  

Link to comment
https://forums.phpfreaks.com/topic/158102-learning-oop-programming/
Share on other sites

I would say you missed the methodology. You can easily handle that with a function instead of a class, why would a class be needed to verify login information?

 

A user class, to me, would take in a userid or username. From that go into the database and retrieve that information and store it in the class for use later on. So you would have a property of username, fullname, email, preference1, preference2 etc.. Then methods that you can "set" those preferences, if this was for a "control panel page". If the preference's have been set you have a "save" method to update the database with the new values.

 

If you want to make sure the user is authenticated so they can save/write to the database add a method to check if the user is valid from when they logged in with a session variable. But honestly I do not think you need to (or should) put a login check inside a class. Make that a function, or I guess make it a method inside the user class and when they login populate that class with their userdata for use.

 

Hope that helps.

what I thought premiso was saying, was that this method you used is rather roundabout...and I agreee if it really is that simple of a class... looks good though...

 

although you did not declare

 

username & password as private or public or (internal?) so your username and password (class properties) might be public and available without reuiring to use your getter or setter method..... :-) just something to think about...

 

someone correct me if im wrong...

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.