PostgreSQL & PHP Tutorials - Extending Your API

PHP »  Introduction to Object Oriented Programming

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

For a 'news' api, you'd set up something like this (of course this is an extremely simple example, in your cms you will have extra fields and options to use).

I prefer to keep each API class in separate files. This isn't necessary, but it's easier to do so you can quickly find the right code to edit.


<?php

// require our parent class.
require_once('api.php');

class 
News_API extends API {

    
/**
    * These variables don't exist in the parent class, but we can use them here.
    */
    
var $newsid 0;
    var 
$title '';
    var 
$description '';

    
/**
    * Our constructor. This sets up our database connection.
    * If we pass in an ID to load, it will automatically try to load that news item.
    */
    
function __construct($newsid=0) {
        
$this->SetupDB();
        if (
$newsid 0) {
            
$this->Load($newsid);
        }
    }

    
/**
    * Our constructor. This one is called by PHP4.
    * We just need to pass the newsid along to the PHP5 constructor for it to automatically load.
    */
    
function News_API($newsid=0) {
        
$this->__construct($newsid);
    }

    
/**
    * This creates a news item in the database.
    * It makes sure 'title' and 'description' are set before it does anything else.
    */
    
function Create() {
        
// make sure we're not going to create an empty entry.
        
if ($this->title == '' || $this->description == '') {
            return 
false;
        }
        
$query "INSERT INTO news(title, description) VALUES('" pg_escape_string($this->title) . "', '" pg_escape_string($this->description) . "')";
        
$result pg_query($query);

        
// did the query work??
        
if (!$result) {
            return 
false;
        }
        return 
true;
    }

    
/**
    * This loads a news item from the database.
    * If it loads ok, it sets the 'newsid', 'title' and 'description' class variables.
    * It will automatically prepare the class variables for displaying on a html page.
    */
    
function Load($newsid=0) {
        
$newsid = (int)$newsid// make sure it's an integer for loading.
        
if ($newsid <= 0) {
            return 
false;
        }
        
$query "SELECT title, description FROM news WHERE newsid='" $newsid "'";
        
$result pg_query($query);
        if (
pg_num_rows($result) <> 1) {
            
// found either 0 or multiple results?? that's not good.
            
return false;
        }
        
$row pg_fetch_assoc($result);
        
$this->title htmlentities($row['title']);
        
$this->description htmlentities($row['description']);
        
$this->newsid $newsid;
        return 
true;
    }

    
/**
    * This updates a news item in the database.
    * It makes sure we have already loaded an item (we can't update something that isn't loaded).
    * If it's not loaded, it will return false.
    * If it has been loaded, we can update the database.
    */
    
function Update() {
        
// has the item been loaded?
        // if not, we can't update it.
        
if ($this->newsid <= 0) {
            return 
false;
        }
        
$query "UPDATE news SET title='" pg_escape_string($this->title) . "', description='" pg_escape_string($this->description) . "' WHERE newsid='" $this->newsid "'";
        
$result pg_query($query);

        
// did the query work??
        
if (!$result) {
            return 
false;
        }
        return 
true;
    }

    
/**
    * This method deletes 'this' news item from the database (based on $this->newsid)
    * If it gets deleted, this returns true.
    * If it can't be deleted, this returns false.
    */
    
function Delete() {
        
// has the item been loaded?
        // if not, we can't delete it.
        
if ($this->newsid <= 0) {
            return 
false;
        }
        
$query "DELETE FROM news WHERE newsid='" $this->newsid "'";
        
$result pg_query($query);

        
// did the query work??
        
if (!$result) {
            return 
false;
        }

        
// reset our class variables "just in case".
        
$this->newsid 0;
        
$this->title '';
        
$this->description '';
        return 
true;
    }

}

?>


Note that because the 'News_API' extends the base API, we don't need to create a 'Get', 'Set' or 'SetupDB' method. They exist in the parent and aren't needed in the child unless they do something different.
Viewing page 4 of 5
« | Back | Next | »



Avg Rating: 3
Vote Count: 27


              

  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  : 9 + 4 =
Comment