简体   繁体   中英

How do I make this object-oriented?

Currently I have the code below which registers a user.

It doesn't check to see if the username currently exists or anything like that, that is something that I want to implement.

I've never known how to use php objects and forms together. Any help will be much appreciated.

register.php

The page checks to see if a user is already logged in, either way the form is still displayed and submits to itself. The database access details are stored in config.php as constants.

<?php
session_start();

include("includes/config.php");

if(isset($_SESSION['username'])) {
    echo "You are currently logged in as: " . $_SESSION['username'];
    echo "<br />";
    include("nav.php");
    echo "<hr />";
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
<?php

$odb = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USER, DB_PASS);





if (isset($_POST['firstName'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $password = md5(DB_SALT.$password);
    $type = $_POST['type'];
    $date=date("Y-m-d");                
    $time=date("H:i:s");

    $sql = "INSERT INTO tblMembers (firstName, lastName, username, passwordHash, type, joinedDate, joinedTime, lastActiveDate, lastActiveTime) VALUES (:firstName, :lastName, :username, :passwordHash, :type, :joinedDate, :joinedTime, :lastActiveDate, :lastActiveTime);";
    $query = $odb->prepare($sql);
    $results = $query->execute(array(
        ":firstName" => $firstName,
        ":lastName" => $lastName,
        ":username" => $username,
        ":passwordHash" => $password,
        ":type" => $type,
        ":joinedDate" => $date,
        ":joinedTime" => $time,
        ":lastActiveDate" => $date,
        ":lastActiveTime" =>$time
        ));
}
?>
<form method="post" action="">
    Name: <input type="text" id="firstName" name="firstName" value="Michael" /><br />
    Last Name: <input type="text" id="lastName" name="lastName" value="Norris" /><br />
    Username: <input type="text" id="username" name="username" value="mstnorris" /><br />
    Password: <input type="password" id="password" name="password" value="password" /><br />
    Type: <input type="text" id="type" name="type" value="4" /><br />
    <input type="submit" value="Add" />
</form>
</body>
</html>

I know how to write php objects using classes. This is what I had previously although I have been told that the methods I used are outdated. If anyone can shed any light on how to update it, it sure would help.

<?php

require_once("database.php");

class Member extends DatabaseObject {
    protected static $table_name = "tblMembers";
    var $firstName = "Mike"; // initiating the $firstName variable
    var $lastName = "Norris"; // initiating the $lastName variable
    var $username = "mstnorris"; // initiating the $username variable
    var $password = "password"; // initiating the $password variable
    var $reviews = "0"; // initiating the $reviews variable
    var $type = "4"; // initiating the $type variable

    function __construct($firstName, $lastName, $username, $password, $reviews, $type) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->username = $username;
        $this->password = $password;
        $this->reviews = $reviews;
        $this->type = $type;
        //$this->insert($firstName, $lastName, $username, $password, $type);
    }

    function set_firstName($firstName) {
        $this->firstName = $firstName;
    }

    function get_firstName() {
        return $this->firstName;
    }

    function set_lastName($lastName) {
        $this->lastName = $lastName;
    }

    function get_lastName() {
        return $this->lastName;
    }

    function get_fullName() {
        if (isset($this->firstName) && isset($this->lastName)) {
            return $this->firstName . " " . $this->lastName;    
        } else {
            return "";
        }
    }

    function set_username($username) {
        $this->username = $username;
    }

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

    function set_password($password) {
        $this->password = md5(DB_SALT.$password);
    }

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

    public static function authenticate($username="", $password="") { 
        global $database;
        $username = $database->escape_value($username);
        $password = $database->escape_value($password);
        $passwordHash = md5(DB_SALT.$password);

        $sql = "SELECT * FROM tblMembers ";
        $sql .= "WHERE username = '{$username}' ";
        $sql .= "AND passwordHash = '{$passwordHash}' ";
        $sql .= "LIMIT 1";

        $result_array = self::find_by_sql($sql);
        if (!empty($result_array)) {
            //echo "true";
            return array_shift($result_array); // Pulling first element from array
        } else {
            //echo "false";
            return false; // Ability to ask whether we return something
        }

    }

    public function insert($firstName, $lastName, $username, $password) {
        $database = new Database();
        $database->query("INSERT INTO tblMembers VALUES ('','{$firstName}','{$lastName}','{$username}','{$password}','4')");
    }

    // Common Database Methods

    private static function instantiate($record) {
        $object = new self;

        foreach ($record as $attribute=>$value) {
            if ($object->has_attribute($attribute)) {
                $object->$attribute = $value;
            }
        }
        return $object;
    }

    public static function find_all() {
        return self::find_by_sql("SELECT * FROM ".self::$table_name);
    }

    public static function find_by_id($id=0) {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE userID={$id} LIMIT 1");
        if (!empty($result_array)) {
            return array_shift($result_array); // Pulling first element from array
        } else {
            return false; // Ability to ask whether we return something
        }
    }   

    public static function find_by_sql($sql="") {
        global $database;
        $result_set = $database->query($sql);
        $object_array = array();
        while ($row = $database->fetch_array($result_set)) {
            $object_array[] = self::instantiate($row);
        }
        return $object_array;
    }

    private function has_attribute($attribute) {
        $object_vars = get_object_vars($this);
        return array_key_exists($attribute, $object_vars);
    }
}

?>

Can the MVC approach be used with AJAX? Also, with that in mind, the AJAX code I have used before in other projects use $_GET, is there any problems with this as the data is never being sent to the address bar? If so, how do I use $_POST with AJAX?

Mike:

your set a getter and a setter like this:

class Spam
{
    public $attr;
    public $var;
    public $arg;

    /* __construct, __set, and __get 
      these are all special functions
      we know this from the double underscore */

    function __construct () 
    {
           // construction code
    }

    function __set ( $arg0, $arg1 )
    {    
           $this->$arg0 = $arg1;
    }

    function __get ( $arg )
    {
           return $this->$arg;
    }
}

and you would call it from your code as follows:

// this calls the __constructor function
$barney = new Spam();

// this calls the __set function
$barney->attr = "garnished with spam & eggs";

// this calls the __get function
$attrValue = $barney->attr;

This reduces the need to call a different method to set/get the values of your variable. This will only work on public variables as private and protected variables cannot be accessed from outside of your class.

Also, it is a good idea to have separate views, models, and controllers. Your controller is the script that the form submits to, your model is the class that is instantiated, and your view is where the user sends the information from. This will make your code easier to understand, rather than having your controller and view together.

Are you restricted to PHP4 for some reason? Or did you download some really old code and you're now trying to get it to work?

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<[ UPDATE 2.27.2013 ]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

OOP PHP Programming in Conjunction with JavaScript AJAX technology

Model-View-Controller:

MVC is not specific to PHP. MVC is a software design pattern that aims to solve a maintainability problem in code that combines separate components of the code in ways that make the code less readable and hard to maintain, which in the end is also a security risk. Model-View-Controller is typically implemented via frameworks. In regards to PHP there are frameworks available such as Zend, CodeIgniter, CakePHP, etc. There frameworks implement the model view controller through the document tree, although you can create your own PHP framework (which isn't a good idea given your new to the language), its probably better to use one that has already been around. These frameworks may also enforce their own standards that result in better code.

模型视图控制器

To understand a maintainable MVC framework you should be familiar with coding a website > > entirely in PHP. That means you should be using PHP classes [modules|models] to dynamically generate the HTML pages[the view] depending what the user has done[the controller file controls the model].

You question is very vague and its hard to tell what your asking, however, I get the impression you're unable to figure out what MVC is and how to use it. Suppose you've just created a layout for a website you will be developing. Since it isn't possible to predict the size of your user's screen, you're layout was designed in the browser. Your layout [or template if you will] should be standard compliant HTML5 or XHTML. It should not be constructed with images. Some people may disagree with me on this but with the exception of your logo/header(though some use text for this too), you should not have any tags are part of your template(this is before any content has been written, obviously you'll probably want to use a lot of images in your content). Your view at this point should be HTML and CSS - any images that are a part of your layout (ie patterns, backgrounds, anything layout specific) should be in the CSS of your website. This is kind of the same reason that we use the MVC pattern - it separates what should be separate.

You take your layout as the HTML and you write a PHP class[module] that contains functions, for example we'll use $view->showLeadboard();, $view->showAds(); $view->showFooter(); $view->setTitle("Title");, $view->setDescription("Description");... This assumes that you've instantiated your class. Perhaps you don't want to instantiate a class and you'd prefer to use static methods, the choice is yours but you should understand what you're doing well enough to have good reasons for doing it.

Now that your view is held inside of a PHP module you can worry about your content. Chances are, if your website is dynamic, there will be multiple pages and locations on those pages that contain dynamic content from a database, or forms (we're still inside of the view) that submit data to the controller.

Suppose somebody is registering at your website. The go to your domain and a view is generated based on the request to www.site.com and the view that is generated is the index page. This person who has come to your page has decided to register for an account with your service. They click on the "register" hyperlink and another view is generated that displays a form for them to create their login credentials. They fill the form out click submit. The information supplied in the form is submitted to a controller(we're not talking about ajax or implementing an MVC design pattern for our javascript code right now), we'll say that the view site.com/register submits to the controller site.com/engine/process.php . Process.php filters/sanitizes the user data from the form and instantiates the correct class(model, we'll call this one new User ) that will then make a database call through one of its methods, or maybe even through its constructor(you should be aware of the magic methods available to you in PHP) and this the result of this query mutates the view to be slightly different depending on what the controller told the model and what the model told the view.

I don't even know what I can say about your question regarding AJAX - given your position with PHP I'm going to guess that you're using JQuery for ajax calls. If this is the case you do not need to implement a model-view-controller from your jquery files, you can just create a jquery script and then add a method to your view that calls that script and implements it.

All in all if you are struggling to understand what a common pattern like MVC is and how to use it you should really go back to the basics. I can't stress enough that the online tutorials aren't going to help you if you don't understand why the author used the solution that they used and chances are they're not explaining that to you because its sometime simple that you should be able to understand yourself provided you have a basic understanding of the php language, its syntax, and how to solve problems with it. This all comes just from spending time with the language, learning how it works, and learning what it doesnt do well and what it does do well.

Ok, you have a couple of questions wrapped into one large question but I'll try to answer them as best as I can. I'll answer them in the order of importance.

  1. How do you update your class(es).
  2. How to structure forms better.
  3. How to check login status.
  1. Most applications now use some form of an MVC architecture. Models, Views, and Controllers are a way of separating responsibilities to classes. Here's a brief tutorial on MVC architecture for PHP. With that said, there are a number of open source frameworks that you can use like, Zend , CakePHP and more.

    Try using one of the strategies for MVC or try a framework.

  2. Try not to have the form self submit to itself. Instead route it to a seperate page and handle the logic there. Also you can wrap your inputs into and array by using the [] notation. For example: <input type="text" name="user[firstname]" />

    However If you are just doing a login form, then all you need is some unique form of identification and a credential (eg username and password).

  3. There are several ways to persist users' login status, chiefly used are sessions and cookies. Storing the entire model in the session or cookie is usually frowned upon. Instead try storing the username and a unique key that you can compare against in a database. Using cookies gives you more control over how long you want the session to last.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM