Define class for tracking users in PHP

Description

The following code shows how to define class for tracking users.

Example


/*w w  w .  ja v a2  s .  c  om*/
<?php
  //define class for tracking users
  class User
  {
    //properties
    public $name;
    private $password, $lastLogin;

    //methods
    public function __construct($name, $password)
    {
      $this->name = $name;
      $this->password = $password;
      $this->lastLogin = time();
      $this->accesses++;
    }

    // get the date of the last login
    function getLastLogin()
    {
      return(date("M d Y", $this->lastLogin));
    }
  }

  //create an instance
  $user = new User("Leon", "asdf123");

  //get the last login date
  print($user->getLastLogin() ."<br>\n");

  //print the user's name
  print("$user->name<br>\n");
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition