Categories

.NET (1) CSS (5) OOP (1) PHP (2) SQL (5) SQL Server (8) TSQL (1)

Use of CLASSES in PHP – A simple approach

Prerequisites:  HTML , PHP , Basic of SQL and Basic of OOP

I’ll explain this by using an example of ADDING COURSES. Consider there is a table “Course” with following fields – Code , Name , CreditHrs , Department.
Now I’ll explain the whole flow of insertion of add course using PHP Classes and some concepts of OOP.

Consider there is a form in which we will insert all these fields.
     
<form  action=”xyz.php” method=”POST”>
</form>

I’m not writing the code of HTML FORM. It is very simple. I’m giving only its PHP code. Before going to its handle (action ) file let’s make classes first. I’m making two classes one is “course” and other is “CourseDAO”. Course class will contain all the getter and setters and CourseDAO will handle all DB insertion/updation/deletions etc.

<?php
class course
{
     private $Code;
     private $Name;
     private $CreditHrs;
     private $Department;             
            function __construct()
            {           
            }           
            
            /* ----------  Getter  ---------- */           
            
     public function getCode()
     {return $this->Code;}
            
     public function getName()
     { return $this->Name; }
     public function getCreditHrs()
     { return $this->CreditHrs; }
     public function getDepartment()
     { return $this->Department; }
   
    
     /* ----------  Setter  ---------- */
            
            
     public function setCode($n)
     {$this->Code=$n;}    
    public function setName($n)
     {$this->Name=$n;}
     public function setCredithrs($n)
     {$this->CreditHrs=$n;}
      public function setDepartment($d)
      { $this->Department=$d; }       
             }?>


 COUSRSE DAO class

<?php

require_once(“queries.php");
require_once("connection.php");
require_once("course.php");

class CourseDAO
{
public function insertCourse($c)
{            $con=new Conncetion();          
$name=$c->getName();
$c1=$c->getCode();
$crh =$c->getCreditHrs();
$dp=$c->getDepartment();    
    
     $con->getconnected();
    
     $query=INSERT_COURSE.$c1.",'".$name."','".$crh."','".$dp."')";
     $result=mysql_query($query)or die("Error in query: $query. ". mysql_error());
           
            return true;
}

Code explanation

In DAO class I included three files.

require_once(“queries.php");
require_once(“connection.php");
require_once(“course.php");

I included course.php because I’ll using its getter and setters.

Connections.php is

<?php
INCLUDE("config.php");

class Conncetion{
 public function getconnected()
            {
                  $link_id=mysql_connect(host,username,pass);
                   if(mysql_select_db(db))
                   {                 }
                   else { die("connection failed"); }
          } //end of function
}
?> 


It includes “config.php”. Config.php defines only DB constants

<?php
define("host","localhost");
define("username","root");
define("pass","");
define("db","dbname");
?>




Now consider the  queries.php.  I just write queries separately so that if there is some change in queries so that I can change it in one place. You can write queries directly into the DAO file.

define("INSERT_COURSE","INSERT INTO ".TABLE_COURSE."(Code,Name,CreditHrs,TimTid,Department)VALUES(");


In queries file I just define the constant “INSERT_COURSE” and assign it a value, which is insert query  actually.
Similarly I defined tables name in separate files so that if there is some change in table name then we can change it from only one place. In table names file I just define constant like

<?php

     define("TABLE_COURSE","course");

?>


Now consider the file xyz.php where form values will be posted.


<?php

include("course.php");
include("courseDao.php");

$c=new course();
$cDao=new CourseDAO();


$c->setName($_GET['name1']);
$c->setCode($_GET['ccode1']);
$c->setCredithrs($_GET['ch1']);
$c->setTimtid($_GET['ttid1']);
$c->setDepartment($_GET['dpp1']);
//echo $c->getCode();

if($cDao->insertCourse($c))
{echo "<div id='su'>seccessfully inserted</div>";}

?>



I simply made  two objects. One of course and other of DAO. Now I set the values of course object using their setters and then I just called the insertCourse function and passed it course object as a parameter. And then in insertCourse function in DAO I simply get the values of course and then run the query.

This is how we can use OOP features in PHP. I tried it to keep it very simple.