Page 1 of 1

what's the diff between using a class and not using a class?

Posted: Tue Mar 03, 2009 3:40 am
by markthien
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


hi guys,

For instance, I got a login process like below:

login.php
------------------------------------------------------------------

Code: Select all

<form id="login_form" action="process-login.php" method="post">
        <p>
            <label for="name">Username: </label>
            <input type="text" name="username" />
        </p>
        
        <p>
            <label for="pwd">Password: </label>
            <input type="password" name="pwd" />
        </p>
        
        <p>
            <input type="submit" id="submit" value="Login" name="submit" />
        </p>
</form>
then forward the request to process-login.php like below
-----------------------------------------------------------------

Code: Select all

<?php
$con = null;
 
try {
$con = new PDO('mysql:host=localhost;dbname=abc', 'user1', 'password');
 
} catch(PDOException $e) {
    error_log($e->getMessage());
}
 
$valid_user = false;
$username = $_POST['username']);
$password = $_POST['pwd']);
 
try {
    $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $con->prepare('select count(*) from user where username = :username and password = :password');         
    $stmt->execute(array('username' => $username , 'password' => md5($password)));
    if($stmt->fetch()) {
        $stmt->close();
        $valid_user = true;
    }   
} catch(PDOException $e) {
    error_log($e->getMessage());
}
 
if($valid_user){
   echo 'success';
}
 
$con = null;
?>
versus forwarding to process-login.php like below
-----------------------------------------------------------------

Code: Select all

<?php
 
session_start();
require_once 'classes/Membership.php';
$membership = new Membership();
 
if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) {
    $response = $membership->validate_User($_POST['username'], $_POST['pwd']);
}
 
if($response)
  echo 'success';
else
  echo 'fail';
 
?>
Membership.php
-----------------------------------------------------------------

Code: Select all

<?php
 
require 'Mysql.php';
 
class Membership {
    
    function validate_user($un, $pwd) {
        $mysql = New Mysql();
        $ensure_credentials = $mysql->verify_Username_and_Pass($un, md5($pwd));
        
        if($ensure_credentials) {
            $_SESSION['status'] = 'authorized';
            header("location: index.php");
        } else return "Please enter a correct username and password";
        
    } 
    
    function log_User_Out() {
        if(isset($_SESSION['status'])) {
            unset($_SESSION['status']);
            
            if(isset($_COOKIE[session_name()])) 
                setcookie(session_name(), '', time() - 1000);
                session_destroy();
        }
    }
    
    function confirm_Member() {
        session_start();
        if($_SESSION['status'] !='authorized') header("location: login.php");
    }
    
}
?>
Mysql.php
-----------------------------------------------------------------

Code: Select all

<?php
 
require_once 'includes/constants.php';
 
class Mysql {
    private $con;
    
    function __construct() {
        try {
            $this->$con = new PDO('mysql:host=localhost;dbname=abc', 'user1', 'password') or die('failed to connect to database');
            $this->$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e) {
            error_log($e->getMessage());
        }                     
    }
    
    function verify_Username_and_Pass($username, $password) {   
        
        $stmt = $con->prepare('select count(*) from user where username = :username and password = :password');         
        $stmt->execute(array('username' => $username , 'password' => md5($password)));
        if($stmt->fetch()) {
            $stmt->close();
            return true;
        }
        return false;
    }
}
?>
What I mean is what is the different between using an OO way and a straight forward way?

thanks & regards,
Mark


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: what's the diff between using a class and not using a class?

Posted: Tue Mar 03, 2009 9:57 am
by pickle
Do a search on these forums for procedural vs oop. This has been discussed fairly well previously. Basically it boils down to personal preference & cleanliness of code.

Re: what's the diff between using a class and not using a class?

Posted: Tue Mar 03, 2009 11:17 am
by Paul Arnold
OOP tends to be easier to maintain and extend but can be overkill for smaller tasks.

It's well worth learning OOP (I'm still trying) as it can be massively beneficial and save a lot of time on certain tasks that you do a lot.

Procedural tends to be easier for beginners to understand and follow.