Encapsulating the PDO Object

in #programming3 years ago

encapsulation.jpg

Essentially, the term "encapsulate" means placing in a container or capsule. In computer science, programmers often write programs that encapsulate the interface for another program simply to fit a different style of programming.

This picture for this page shows a lightbulb placed inside a jar to convey the concept of encapsulation.

The PHP PDO object is an interface that encapsulates database connections. It presents the connection in the way that the people at Zend Technologies finds desirable.

I do not want to adopt the Zend programming style. So, I created a set of programs that encapsulates the PDO object. This is a plain text file

This code viewer shows the function embedded in a test page. There is a line in viewer called "require('/var/www/php/sql.php');" Clicking this line toggles your view of the sql.php program.

sql.php is 339 lines. It contains the following programs:

  • dbConn() creates PDO objects that connect to different databases. The program holds an array of databases as a static variable. This way I can connect to new databases on the fly in a program. dbConn() returns a PDOStatement.
  • sqlValue() performs a SQL SELECT statement and returns a single variable. I pass the variables to the sql statement as an array.
  • sqlRow() performs a SQL SELECT and returns a single row of data.
  • sqlAll() performs a SQL SELECT and returns an array. I might change the name to sqlArray(). I made the change in the text file.
  • sqlExec() executes SQL UPDATE, REPLACE and INSERT statements. Again, I pass the variables in an array.

The functions call the message object that I referred to yesterday.

Programming with these functions is a breeze.

If I need a single value from the database I simply call sqlValue(). I can make the call inline:

``

 // This code will say hello to user 25
 echo 'Hello '.sqlValue('SELECT name FROM User_File WHERE user_id=?'.[user_id]);

``

In the next code, I get the address for a user as a row which I parse with the PHP list() function

``

$sql = 'SELECT name, address, city, city, zip FROM User_Fie WHERE user_id=?';
list($name,$address,$city,$state,$zip) = sqlRow($sql,[$user_id]);

echo $name.'
'.$address.'
'.$city.', '.$state.' '.$zip;

``

I can get a whole table with sqlArray(). I've found that I prefer processing data as an array than line by line as a PDOStatement.

Updating the database is child's play as well. I simply write an UPDATE, INSERT and REPLACE statement and call sqlExec().

The message object follows the status of the calls to the database. I can start transactions with dbConn. If there is an error writing to the database, the program will set the error toggle, roll back the transaction and prevent further transactions. The message object logs the error.

The four functions are incredibly easy to use and make for a robust programming environment.

In my opinion, programming interfaces should make programming easier.

The PDO object produced by Zend makes it incredibly difficult to create data driven web sites.

Anyway, this post was about encapsulation. The primary for encapsulating the interfaces to different resources is that, if done correctly, the encapsulation will create a system which is dynamic and easy to maintain.

This code is very interesting. The code encapsulates the Zend's PDO object.

Don't you see? I am encapsulating code written with the class structure and presenting it as a function. I am taking things in the opposite direction than that dictated by the technorati.

IMHO, the reason to encapsulate resources is to simplify things.

The next post will show the way that I extend my database functions to create data driven web pages.