Building A House In PHP

No Comments »

PHP, though having it’s limitations, has grown into a vast and robust language of choice by many developers, including myself.  PHP5 when initially released back in 2006, offered a true Object Oriented experience.  Yet to this day, I still find that many PHP develpers are not using the true capabilities that PHP has to offer.  Frameworking in PHP is not only ideal, but offers a great amount of flexibility and power to drive any system.  Granted, not all sites will need a robust framework, having a psuedo framework in place is still idea.

The days of inter-twining PHP and HTML should be something of the past, yet I find many still do it.  Even without creating a J2EE Framework, a small psuedo framework could be something that will help you rapidly deploy the site in a matter of days to a week.  Like most mainstream software engineering is concerned, you have several layers.  You will have the view or graphical interface layer, the database abstraction layer, and the processing layer.  Properly setting up classes and objects to handle each layer can greatly improve speed, reliability, and ease of trouble shooting.

Mixing PHP and HTML makes the code hard to read, hard to understand, and sends you on a needle in a haystack hunt trying to find the break.  Creating a templating layer that will read a template and combined with the processing layer can help you easily find a break when it comes to visual.  You basically know it will be in one of those components.  If you are smart, you will incorporate a debug mode to all of your classes that you can verbose out all information that is being processed and how it is being processed.  The only layer that should ever talk to the visual layer is the constructor page and the content processing layer.  Even then they really don’t need to talk.  The visual object or layer should only have to deal with the visual aspects.  The content processing layer should only have to work with the database layer to get the content ready to be inserted into the visual layer.

This will provide a basic framework that you can use through out all sites that will make it easier to deploy and rapidly deploy sites that do not need the power of an actual framework.  The typical core objects I usually have are the template class, and the content class.  Though the content class will usually be broken up into it’s own classes and subclasses based on the function of the particular page or data being processed.  The template class is basically just that.  It will grab a template, insert the necessary data, and return.  Lately I have been using PDO as a the database layer.  If you have ready my previous post on a better way of using a database object, then you already know that I initiate the database object in an autoload page, and then make that object readily available to all classes.  It cuts database tunnels to only one, and i believe is more efficient on resources.  Not to mention it makes things a whole lot easier.

It provides flexibility in changing templates on the fly, keeps the data where it belongs, and it makes the code easier to read.

Add This! Blinkbits Blinklist Blogmarks BlogMemes BlueDot BlogLines co.mments Connotea del.icio.us de.lirio.us Digg Diigo DZone Facebook FeedMeLinks Folkd.com Fleck Furl Google Google Reader icio.de IndianPad Leonaut LinkaGoGo Linkarena Linkter Magnolia Mister Wong MyShare Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Netvouz Newsgator Newsvine Oneview.de RawSugar reddit Rojo Segnalo Shadows Simpy SlashDot Smarking Sphere Spurl Startaid StumbleUpon TailRank Technorati ThisNext yigg.de Webnews.de ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com Plugin by Dichev.com
PHP, Tutorials, Web Programming | July 3rd 2008

A Better PHP/MySQL Connection

No Comments »

Keeping heavy loads off of a database can be a tedious task in itself.  Many times multiple instances are created, creating multiple connections to the database that hog resources when in fact only one instance and connection is needed.  With PHP5, database abstraction layers are built into the core.  PDO, although it has it’s deficiencies, can be a good starting point for creating a simple database abstraction layer.  However, learning how to use one single instance throughout the life of the page is the key to balancing server load and enhancing performance.

Starting off with a simple “autoload” file I find is helpful.

<?php
include( ‘autoload.php’ );
?>

The autoload file doesn’t have to be complicated.  It can be a basic file that creates all instances to be used through out the page, not to mention autoload classes that can be called else where throughout the script.

autoload.php

<?php
include( ‘config.php’);

// loop to auto load class files

/* Single Database initiation to be used throughout the script */

$dsn = “mysql:host=localhost;dbname=my_database”;
$dbuser = “my_database_username”;
$dbpass = “my_database_password”;

$database = new PDO( $dsn, $dbuser, $dbpass );
?>

Typically it is better to define the database info in a global configuration file that will typically not change.  It keeps all important variables that will be used multiple times in one place.  But this autoload file defines the variable $database that will be used throughout the rest of the page.  This will create on instance and most importantly, 1 connection to the database to be used.

On the file that will be needing, not mention allowing class and objects to use this instance can be accomplished this way:

File:

<?php
include( ‘autoload.php’);

$sql = “select * from my_table”;

$stmt = $database->prepare( $sql );
$stmt->execute();

while( $rows = $stmt->fetchAll() )
{
// Do Something
}
?>

Passing and allowing usage in objects:

<?php
include( ‘autoload.php’ );

$helper = new helper( $database );

echo $helper->displayView();
?>

The class file:

<?php
class helper
{
private $database;

public function __construct( $database )
{
$this->database = $database;
}

public function displayView()
{
// do something
$sql = “some sql statmet”;
$stmt = $this->database->prepare( $sql );
$stmt->execute();

while( $rows = $stmt->fetchAll() )
{
// Do something
}
}
?>

As you can see, this allows one single database thread to be used across the entire page freeing resources on the server making it more efficient.

For more information on PDO visit the manual at http://www.php.net/pdo.

Add This! Blinkbits Blinklist Blogmarks BlogMemes BlueDot BlogLines co.mments Connotea del.icio.us de.lirio.us Digg Diigo DZone Facebook FeedMeLinks Folkd.com Fleck Furl Google Google Reader icio.de IndianPad Leonaut LinkaGoGo Linkarena Linkter Magnolia Mister Wong MyShare Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Netvouz Newsgator Newsvine Oneview.de RawSugar reddit Rojo Segnalo Shadows Simpy SlashDot Smarking Sphere Spurl Startaid StumbleUpon TailRank Technorati ThisNext yigg.de Webnews.de ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com Plugin by Dichev.com
PHP, Tutorials, Web Development, Web Programming | June 24th 2008