PHP Object Interface Primer Tutorial

PostgreSQL Primer Tutorial

PHP Object Interface Primer Tutorial

Today we are going to show a PHP example of object “interface”, and we like the excerpt from the useful link below, which discusses the same “database” interface idea as we are going to tackle today …

The point is – the INTERFACE describes the methods that we need to access our database. It does NOT describe in any way HOW we achieve that. That’s what the IMPLEMENTing class does. We can IMPLEMENT this interface as many times as we need in as many different ways as we need. We can then switch between implementations of the interface without impact to our code because the interface defines how we will use it regardless of how it actually works.

… in that today, on our local MAMP web server, we imagine a web application able to toggle between two different database arrangements for …

  1. PostgreSQL
  2. MySql

… to present some “films” (table) data, as we did a few days ago when we just did a PostgreSQL version with PostgreSQL PHP Tutorial.

So, as you can tell with our PHP source code filminterface.php where we go …


interface Database {
function listFilms();
function addFilm($code, $title, $did, $date_prod, $kind, $len);
function removeFilm($code);
function amendFilm($code, $title, $did, $date_prod, $kind, $len);
}

… with our “Database” interface class, that this interface class shows what functionality occurs in common for classes that “interface” to it via …


class MySqlDatabase implements Database {
// ...
}
class PostgreSQLDatabase implements Database {
// ...
}

Please feel free to examine the code and try a scenario yourself in a Mac OS X MAMP Apache/PHP/MySql (and add your own PostgreSQL (how to install is discussed at PostgreSQL Primer Tutorial way below)) environment.


Previous relevant PostgreSQL PHP Tutorial is shown below.

PostgreSQL PHP Tutorial

PostgreSQL PHP Tutorial

Some time ago we talked about the PostgreSQL database, and showed you some command line (installation and) usage via a MacBook Pro Terminal application’s (bash environment) command line.

So that showed a bit of SQL for PostgreSQL and you can see it is a lot like other database SQL, and am not about to tell you otherwise today.

The only SQL part that may not be transportable to other database SQL today, is the use of the PostgreSQL “to_date()” function, but the lesson is that so much relational database coalesces with the SQL (DML) for …

  • SELECT (query the database) … for reading …
  • INSERT (add record to the database) … for writing …
  • UPDATE (amend a record of the database) … for writing …
  • DELETE (delete record of the database) … for writing (oddly)

… they all have six letters, and know that has made things cute programmatically for me in the past.

So it is good to not let database work phase you for the way you live with it from day to day with the commands above … but what is sometimes more confusing is the way you connect with databases … generally it involves …

  • hostname (and port, perhaps)
  • username
  • password
  • database (mostly)
  • access rules (occasionally)

We’ve talked about PHP and MySql a fair bit at this blog, and the fact is PHP can quite effectively work with PostgreSQL as well, which we show you today with our local (Mac) MAMP local web server, but do not have available at the domain rjmprogramming.com.au, which PHP was not compiled to include module “pgsql” … the PHP command “phpinfo()” will tell you what the state of play is with your Apache/PHP web server with regard to this.

So today, on MAMP, we take up where we left off with PostgreSQL Primer Tutorial as shown below, and write some PHP you could call pg.php to perform SQL (DML) on that “films” table of that tutorial below.

You can INSERT and UPDATE and DELETE to the database, and behind the scenes we SELECT all the database records of the “films” table to keep you up to date with how things sit.

We used the great Gifpal to create an animated GIF (of a MAMP session execution) for your perusal here.

Link to PostgreSQL Database Server more information … via Wikipedia.
Link to PostgreSQL Database Server home … Open Source.
Link to PostgreSQL Database Server … documentation.

Hope you get something out of today’s database (and PHP) offering.


Previous relevant PostgreSQL Primer Tutorial is shown below.

PostgreSQL Primer Tutorial

PostgreSQL Primer Tutorial

Welcome to the PostgreSQL (Database Server) Primer Tutorial.

The PostgreSQL Database Server product is an excellent Open Source database that has been established for many years. PostgreSQL is an object-relational database management system, and read along with Wikipedia, how its emphases are extensibility and standards-compliance.

Doing this tutorial, and please forgive my naivety with PostgreSQL, I was amazed at the DDL similarities with those databases I was more familiar with, namely MySql, MS-SQL, Oracle, Advantage and Access, and can imagine setting up workable systems in a short space of time.

In the primer tutorial you can see early parts of an install to a MacBook Pro and its “psql” command line interface, like the wonderful “sqlplus” interface of Oracle.

Link to PostgreSQL Database Server more information … via Wikipedia.
Link to PostgreSQL Database Server home … Open Source.
Link to PostgreSQL Database Server … documentation.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in Database, eLearning, OOP, Tutorials and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

36 Responses to PHP Object Interface Primer Tutorial

Leave a Reply

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