Articles PHP PHP Code

Object oriented programming in php

In this post i would like to explain about how to implementing object oriented programming in php, How to create class, and how to call the method of that class.

object oriented programming in php | Anil Labs
object oriented programming in php | Anil Labs


 

Class code:

 

<?php
/**********************************************************************
     * Author    :  Anil Kumar Panigrahi
     * E-mail    :   [email protected]
     * Created on:  6th June 2011
     * Version   :  1.0
     * Project   :   Database Interactions
     * Page      :  User Class
     * Company   :   Anil Labs  (http://www.anillabs.com)
     * Modified on :  
     * Modified by :  
*************************************************************************/


class Users{
     
    /*****************************************************************
      Users is a constructor to establish the database connection
     *****************************************************************/

    private $dbHost;
    private $dbUser;
    private $dbPassword;
    private $dbName;
    private $first_name;
    private $last_name;
    private $user_name;
    private $password;
    private $id;
    private $con;
    public function __construct($dbHost,$dbUser,$dbPassword,$dbName){
        $this->dbHost = "{DATABASE SEVER}";
        $this->dbUser = "{USERNAME}";
        $this->dbPassword="{PASSWORD}";
        $this->dbName= "{DBNAME}";
        $mysqli = new mysqli($this->dbHost,$this->dbUser,$this->dbPassword,$this->dbName);
        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }
   
    /*****************************************************************
      insert function to insert data into database
     *****************************************************************/

   
    public function insert ($first_name,$last_name,$user_name,$password){
               
        $stmt1 = $mysqli->prepare("INSERT INTO USERS VALUES (?, ?,?,?)");
        $stmt1->bind_param('ssss', $this->first_name,$this->last_name,$this->user_name,$this->password);
        $this->first_name=$first_name;
        $this->last_name=$last_name;
        $this->user_name=$user_name;
        $this->password=$password;
        $stmt1->execute();
        $stmt1->close();       
        $id = $mysqli->insert_id;
        return $id                     
    }
   
    /*****************************************************************
      udpate function to update data into database
     *****************************************************************/

   
    public function update($id,$first_name,$last_name,$user_name,$password){
       
        $stmt1 = $mysqli->prepare("UPDATE INTO USERS first_name=?,last_name=?,user_name=?,password=? where user_id=?");
        $stmt1->bind_param('ssssi', $this->first_name,$this->last_name,$this->user_name,$this->password,$this->id);
        $this->first_name = $first_name;
        $this->last_name= $last_name;
        $this->user_name= $user_name;
        $this->password= $password;
        $this->id= $id;
        $result=$stmt1->execute();
        $stmt1->close();
        if($result) return true;
        else return false;
                                       
    }
   
    /*****************************************************************
      delete function to dalete data from database
     *****************************************************************/

    public function delete($id){
       $stmt1 = $mysqli->prepare("DELETE FROM USERS WHERE user_id =?");
       $stmt1->bind_param('i', $this->id);
       $this->id= $id;
       $result=$stmt1->execute();
       $stmt1->close();
       if($result) return true;
        else return false;
    }
   
    /*****************************************************************
      retrieve function to get data from database
     *****************************************************************/

     
    public function retrieve($id){
       $stmt1 = $mysqli->prepare("SELECT * FROM USERS WHERE user_id =?");
       $stmt1->bind_param('i', $this->id);
       $this->id= $id;
       $result=$stmt1->execute();
       $stmt->bind_result($first_name, $last_name,$user_name,$password);
       while ($stmt->fetch()) {
        printf ("%s ,%s,%s,%s\n", $first_name, $last_name,$user_name,$password);
       }
       $stmt1->close();
    }
    public __destruct() {
             unset($this->dbHost);
             unset($this->dbUser);
             unset($this->dbPassword);
             unset($this->dbName);
             unset($this->first_name);
             unset($this->last_name);
             unset($this->user_name);
             unset($this->password);
             unset($this->id);
             $mysqli->close();
             unset($this->con);
       }
   
}


?>

It is useful for who is newbie of php classes. This post not useful for advanced programmer.

5 thoughts on “Object oriented programming in php”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.