More with PHP Classes

I got a comment on my PHP OOP tutorial to explain how to relate what I taught to real life scenarios. His scenario was Human Resources Information. The first thing we will need to decide is what info is important, so lets list a few items:

  • First name
  • Last Name
  • Social Security Number
  • Address
  • City
  • State
  • Zip
  • Date of birth
  • Salary
  • Position
  • Start Date

That’s probably sufficient for our purposes, and then some. So the next question we need to ask is, what are we planning on doing with this info? I would assume a lot of it would be interacting with a database. This would probably involve a lot of CRUD operations (Create, Read, Update, Delete). So we need to make functions for each. One to create a new employee in the database, one to get employee info, one to update employee info if it is changed, one to delete employees once they have been fired or never completed training or whatever. The one other thing I can think of is maybe assigning an employee an ID so he can easily be tracked in the system, in case there are two Mr. John Smith employees. Speaking of searching, maybe a search function would be nice as well. So lets set out to create our class:

<?php
class Employee
{
    var $Id=null;
    var $FirstName;
    var $LastName;
    var $DOB;
    var $Position;
    var $Salary;
    var $StartDate=date('m/d/Y');
    var $EndDate=null;
    function _construct($id=null)
    {
        if($id!=null)
            $this->LoadEmployee($id);
    }
    function LoadEmployee($id)
    {
        /*** Connect to database  ***/
        /*** Code Not Included ***/
        $query="SELECT FROM Employees WHERE Id='$id'";
        $result=mysql_query($query);
        //makes sure the user exists
        if(mysql_num_rows($result)==1)
        {
            //Sets class variables
            $row=mysql_fetch_array($query);
            $this->Id=$id;
            $this->FirstName=$row['FirstName'];
            $this->LastName=$row['LastName'];
            $this->DOB=$row['DOB'];
            $this->Position=$row['Position'];
            $this->Salary=$row['Salary'];
            $this->StartDate=$row['StartDate'];
            $this->EndDate=$row['EndDate']
        }else{
            return false;
        }
    }
    function CreateEmployee()
    {
        if(!$this->CheckVariables())
            return false;
        /*** Connect to database  ***/
        /*** Code Not Included ***/
        $query="INSERT INTO Employees (FirstName,LastName,DOB,Position,Salary,StartDate) VALUES ('".$this->FirstName."','".$this->LastName."','".$this->DOB."','".$this->Salary."','".$this->StartDate."')";
        mysql_query($query);
        //Inserted, and now lets return his ID number
        $query2="SELECT Id FROM Employees WHERE FirstName='".$this->FirstName."' AND LastName='".$this->LastName."' AND DOB='".$this->DOB."' AND StartDate='".$this->StartDate."'";
        $row=mysql_fetch_array(mysql_query($query2));
        return $row['Id'];
    }      
    function CheckVariables()
    {
        //makes sure we don't have any empty variables in the areas that matter
        if(empty($this->FirstName)||empty($this->LastName)||empty($this->DOB)||empty($this->Position)||empty($this->Salary))
            return false;
        else
            return true;
    }
    function UpdateEmployee($field, $value, $id=$this->Id)
    {
        //makes sure an ID is chosen or specified.
        if($id==null)
            return false;
        /*** Connect to database  ***/
        /*** Code Not Included ***/
        $query="UPDATE Employees SET $field='$value' WHERE Id='$id'";
        mysql_query($query);
    }
    function ToArray()
    {
        $array['Id']=$this->Id;
        $array['FirstName']=$this->FirstName;
        $array['LastName']=$this->LastName;
        $array['Position']=$this->Position;
        $array['Salary']=$this->Salary;
        $array['DOB']=$this->DOB;
        $array['StartDate']=$this->StartDate;
        $array['EndDate']=$this->EndDate;
        return $array;        
    }
}
?>

Here is the finished class minus a few things that you can add if you want. What my class does is creates employees, updates specific fields, checks information, turns the info into an array, etc. I didn’t include all the variables we listed because I am lazy, as well as the delete function. There are always improvements that can be made, but this is a good starting block for any programmer. So lets see how we use it:

<?php
require("classEmployee.php");
//create a new employee
$emp=new Employee();
$emp->FirstName="Bob";
$emp->LastName="Smith";
$emp->DOB="01/01/99";
$emp->Position="Data Entry";
$emp->Salary="$10/hr";
$empid=$emp->CreateEmployee();
//And now he was created and entered into the system.
echo $empid; //His ID;
//lets check to make sure he is still in the system.
$emp2 = new Employee($empid);
$empArray = $emp2->ToArray();
print_r($empArray);
//ok, all looks good.  Lets update his name.
$emp2->UpdateEmployee("FirstName","Bobby",$empid);
?>

Well that’s my sample. Its pure rough draft, and probably won’t even work properly, but I am conveying the IDEA of OOP and employees. Think how much smoother your code would be if you created a few forms asking for employees IDs and it loads it, or allows the HR Department to enter a new employees info straight into the database on a very simple form. Good luck with this! Let me know if there are any other problems I can help with!