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.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

5 Responses

  1. Pardeep Beniwal says:

    Nice programming..!

  2. Muhammet says:

    Thanks for your code…

  3. Marcos says:

    pt-br: Muito bom esse blog, POO é o que há no momento!

    en: Very good this is blog, OO is better on the moment!

  1. June 14, 2011

    […] Web service for login method. Code is continue with user class which i mentioned in my previous post object oriented programming in php […]

  2. June 9, 2012

    […] 3)Object oriented programming in php […]

Leave a Reply

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