Home > Articles > Object oriented programming in php

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.

You may also like
Website uptime monitoring using PHP script
Understanding the basic need of MVC architecture
How to convert feeds to html using javascript and php
Multi-language support for web application with MySql and PHP

5 Responses

  1. Pingback : How to write a webservices using php with json format | ANIL KUMAR PANIGRAHI 's Blog

  2. Pingback : Top 10 most viewed posts in anil labs - Anil Labs | ANIL KUMAR PANIGRAHI 's Blog

Leave a Reply

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