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:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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 Comments

Pardeep Beniwal · June 23, 2011 at 9:23 am

Nice programming..!

Muhammet · September 24, 2011 at 6:39 pm

Thanks for your code…

Marcos · October 15, 2011 at 2:09 am

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

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

How to write a webservices using php with json format | ANIL KUMAR PANIGRAHI 's Blog · June 14, 2011 at 10:41 am

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

Top 10 most viewed posts in anil labs - Anil Labs | ANIL KUMAR PANIGRAHI 's Blog · June 9, 2012 at 9:56 am

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

Leave a Reply

Avatar placeholder

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