Object oriented programming in php

in Articles, PHP, PHP Code / 5 Comments

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.

With more than 9 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]

Discussion 5 Comments

  1. Marcos

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

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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>