PostgreSQL & PHP Tutorials - Creating an API

PHP »  Introduction to Object Oriented Programming

Posted By Chris Smith Posted on 19 Feb 2006, 07:13 AM
Viewing page 3 of 5
« | Back | Next | »

Let's do something a little more interesting. OOP is fantastic when creating an 'API' (application programming interface) which will allow other developers to perform certain functionality.

I use at least these methods:

'Create' - this creates a new 'item' and saves it in the database.

'Load' - this loads an item ready for use.

'Update' - this updates the item in the database.

'Delete' - this deletes an item from the database.

So, a base API class would look like:


<?php
class API {

    
/**
    * This class variable is set by SetupDB
    */
    
var $DBConnection null;

    
/**
    * In the base class, the constructor doesn't do anything.
    */
    
function __construct() {
    }

    
/**
    * In the base class, the constructor doesn't do anything.
    * This is called by PHP4.
    */
    
function API() {
        
$this->__construct();
    }

    
/**
    * This will set up DBConnection ready for use.
    */
    
function SetupDB() {
        if (
is_null($this->DBConnection)) {
            
$connection_string 'dbname=test';
            
$connection pg_connect($connection_string);
            
$this->DBConnection $connection;
        }
    }

    
/**
    * This is overridden by the child class(es).
    */
    
function Create() {
        echo 
"Can't call 'create' on the base API class<br/>";
        return 
false;
    }

    
/**
    * This is overridden by the child class(es).
    */
    
function Load() {
        echo 
"Can't call 'load' on the base API class<br/>";
        return 
false;
    }

    
/**
    * This is overridden by the child class(es).
    */
    
function Update() {
        echo 
"Can't call 'update' on the base API class<br/>";
        return 
false;
    }

    
/**
    * This is overridden by the child class(es).
    */
    
function Delete() {
        echo 
"Can't call 'delete' on the base API class<br/>";
        return 
false;
    }

    
/**
    * This gets a class variable after making sure it is available.
    * If it's not a set class variable (ie it doesn't exist), then this will return false.
    */
    
function Get($getname='') {
        if (!isset(
$this->$getname)) {
            return 
false;
        }
        return 
$this->$getname;
    }

    
/**
    * This sets a class variable after making sure it is available.
    * If it's not a set class variable (ie it doesn't exist), then this will return false.
    * Otherwise the variable is set and this will return true.
    */
    
function Set($setname=''$value='') {
        if (!isset(
$this->$setname)) {
            return 
false;
        }
        
$this->$setname $value;
        return 
true;
    }
}

?>
Viewing page 3 of 5
« | Back | Next | »



Avg Rating: 3
Vote Count: 28


              

  New Reply


I just have a comment/question. Why dont you implement abstract classes into this example rather than saying you cannot access update/delete etc. from the API class. I would make them abstract, explain a bit about what it is.

[ Editor's note - Interested in submitting an article about it? ]
Joseph Crawford 09 Jun 2006 Reply

This tutorial is very nice, I am trying to read up on creating my own API so the way you have structured things has helped a lot. Carlton Dickson 18 Jul 2007 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 8 + 8 =
Comment