PHP Advanced Concepts

Object Oriented Programming

Everything that you can do with Object Oriented programming (OOP), you can also do using functions/includes, so why learn it? There are two reasons it is a good idea to understand OOP.

First, unknown to many PHP programmers is the fact that just about everything in PHP is an object. A variable is an object, an array is an object, a string is an object, a function is an object, even an object is an object. Most of the object oriented programming regarding all these objects goes on behind the scenes, so most of us don't know that we're dealing with objects as much as we are. Still, to fully understand PHP, it helps to understand objects.

Second, there is a plethora of free downloadable scripts that do everything imaginable on inumerable sites on the internet that are written in OOP. There are also sites that use functions/includes, but to use both kinds of free scripts you must understand objects.

For instance, go to Biz Partner site. I've downloaded their MySQL Database Abstraction Class file, and reproduced it below:

<?php /* Sample usage First enter your host name, username, password and database name in GetHost(), GetUsername(), GetPassword() and GetDBName() functions below. Security notes - You may want to move GetHost(), GetUsername(), GetPassword() and GetDBName() to another file for better security. // Selecting records $DB = new Database(); $DB->OpenConnection(Database::GetHost(), Database::GetUsername(), Database::GetPassword(), Database::GetDBName()); $Query = "SELECT * FROM Table WHERE Field1 = 'Value1'"; $DB->SelectQuery($Query); $DB->CloseConnection(); // Records are stored in the array variable $Result // Number of records selected is stored in the variable $NumRows // Inserting record $DB = new Database(); $DB->OpenConnection(Database::GetHost(), Database::GetUsername(), Database::GetPassword(), Database::GetDBName()); $Query = "INSERT INTO Table (Field1, Field2) VALUES ('Value1', 'Value2')"; $DB->ExecuteQuery($Query); $DB->CloseConnection(); // Updating records $DB = new Database(); $DB->OpenConnection(Database::GetHost(), Database::GetUsername(), Database::GetPassword(), Database::GetDBName()); $Query = "UPDATE Table SET Field1 = 'Value1' WHERE Field2 = 'Value2'"; $DB->ExecuteQuery($Query); $DB->CloseConnection(); // Number of records affected is stored in the variable $AffectedRows // Deleting records $DB = new Database(); $DB->OpenConnection(Database::GetHost(), Database::GetUsername(), Database::GetPassword(), Database::GetDBName()); $Query = "DELETE FROM Table WHERE Field1 = 'Value1'"; $DB->ExecuteQuery($Query); $DB->CloseConnection(); // Number of records affected is stored in the variable $AffectedRows // Error message for any error in manipulating the database is stored in the variable $ErrorMessage */ class Database { var $Connection; var $DBName; var $Result; var $NumRows; var $AffectedRows; var $ErrorMessage; // Gets the name of the MySQL host
function GetHost() { return 'mysql.domain.com'; }
// Gets the username to log into MySQL database
function GetUsername() { return 'username'; }
// Gets the password to log into MySQL database
function GetPassword() { return 'password'; }
// Gets the MySQL database name
function GetDBName() { return 'db'; }
// Class constructor
function Database() {
     $this->Connection = FALSE;
     $this->DBName = '';
     $this->Result = array();
     $this->NumRows = 0;
     $this->AffectedRows = 0;
     $this->ErrorMessage = '';
  }
// Opens connection to specified host, username, password and database name
function OpenConnection($Host, $User, $Password, $DBName) {
     $Success = TRUE;
     $this->Connection = @mysql_connect($Host, $User, $Password);
     if(!$this->Connection) {
        $this->SetErrorMessage();
        $Success = FALSE;
     }
     else {
        $this->DBName = mysql_select_db($DBName, $this->Connection);
  
        if(!$this->DBName) {
           $this->SetErrorMessage();
           $Success = FALSE;
        }
     }
     return $Success;
  }
// Closes current connection
function CloseConnection() {
     $Success = TRUE;
     if($this->Connection) {
        if(!mysql_close($this->Connection)) {
           $this->SetErrorMessage();
           $Success = FALSE;
        }
     }
     return $Success;
  }
// Performs a SELECT SQL query
function SelectQuery($SQL) {
     $Success = TRUE;
     if(!$this->Connection) {
        $Success = FALSE;
        return $Success;
     }
     $Query = mysql_query($SQL);
     if(!$Query) {
        $this->SetErrorMessage();
        $Success = FALSE;
     }
     else {
        $this->NumRows = mysql_num_rows($Query);
        if($this->NumRows > 0) {
           $this->Result = array();
           for($i = 0; $i < $this->NumRows; $i++) {
              $this->Result[$i] = mysql_fetch_array($Query);
           }
        }
     }
     return $Success;
  }
// Performs a non-SELECT SQL query, such as UPDATE and INSERT
function ExecuteQuery($SQL) {
     $Success = TRUE;
     if(!$this->Connection) {
        $Success = FALSE;
        return $Success;
     }
     $Query = mysql_query($SQL);
     if(!$Query) {
        $this->SetErrorMessage();
        $Success = FALSE;
     }
     else {
        $this->AffectedRows = mysql_affected_rows();
     }
     return $Success;
  }
// Get functions to retrieve attributes
function GetConnection() { return $this->Connection; }

  function GetDBName() { return $this->DBName; }

  function GetResult() { return $this->Result; }

  function GetNumRows() { return $this->NumRows; }

  function GetAffectedRows() { return $this->AffectedRows; }

  function GetErrorMessage() { return $this->ErrorMessage; }
// Set functions to set attributes
function SetConnection($Connection) { $this->Connection = $Connection; }

  function SetDBName($DBName) { $this->DBName = $DBName; }

  function SetErrorMessage() {
     $this->ErrorMessage = 'Error ' . mysql_errno($this->Connection) . ': ' . mysql_error($this->Connection);
  }
}
?>

If you edit the host/user/password/database inputs, save the above Class in its own file, say database.inc or something similar, and include the file on whatever page you need to connect to the database, then you have only to do as it says in the comments above to get the benefits of this Class.


Reports   < <  PREVIOUS   Table of Contents NEXT  > >   Anatomy of a Class

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,147 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHPadv1.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 11:12 am, UTC
This file took 0.08455 seconds to process.