PHP
  Home arrow PHP arrow Page 4 - Design Patterns and PHP 5
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Design Patterns and PHP 5
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-09-28

    Table of Contents:
  • Design Patterns and PHP 5
  • The Adaptor Pattern
  • More on the Adapter Pattern
  • The Template Pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Design Patterns and PHP 5 - The Template Pattern


    (Page 4 of 4 )

    The Template pattern describes a class that modifies the logic of a subclass to make it complete.

    You can use the Template pattern to hide all the database-specific connection parameters in the previous classes from yourself. To use the class from the preceding section, you need to constantly specify the connection parameters:

    <?php
    require_once 'DB.inc';
    define('DB_MYSQL_PROD_USER', 'test');
    define('DB_MYSQL_PROD_PASS', 'test');
    define('DB_MYSQL_PROD_DBHOST', 'localhost');
    define('DB_MYSQL_PROD_DBNAME', 'test');
    $dbh = new DB::Mysql(DB_MYSQL_PROD_USER,
    DB_MYSQL_PROD_PASS, DB_MYSQL_PROD_DBHOST,
    DB_MYSQL_PROD_DBNAME); $stmt = $dbh->execute("SELECT now()"); print_r($stmt->fetch_row()); ?>

    To avoid having to constantly specify your connection parameters, you can subclass DB_Mysql and hard-code the connection parameters for the test database:

    class DB_Mysql_Test extends DB_Mysql {
    protected $user  = "testuser";
    protected $pass  = "testpass";
    protected $dbhost = "localhost";
    protected $dbname = "test"; 
    public function _ _construct() { }
    }

    Similarly, you can do the same thing for the production instance:

    class DB_Mysql_Prod extends DB_Mysql {
    protected $user  = "produser";
    protected $pass  = "prodpass";
    protected $dbhost = "prod.db.example.com";
    protected $dbname = "prod"; 
    public function _ _construct() { }
    }

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Advanced PHP Programming," published by...
     

    Buy this book now. This article is excerpted from chapter two of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT