PHP
  Home arrow PHP arrow PHP Application Development Part Two
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

PHP Application Development Part Two
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 33
    2005-03-08


    Table of Contents:
  • PHP Application Development Part Two
  • Storing Dynamic Data
  • Error Handling
  • Event Logging

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    PHP Application Development Part Two
    ( Page 1 of 4 )

    Building on what we learned in part one, this article discusses data storage, system configuration, error handling and logging. Read on to find out how practicing the proper use of these concepts can simplify application maintenance and testing, and accelerate system debugging.

    In the first article of this series, we covered some fundamental PHP application development concepts. Directory structure, file naming, and basic coding conventions were discussed. If you missed that article, you can find it here (http://www.devshed.com/c/a/PHP/PHP-Application-Development-Part-One/). In this article, we continue with the basics of PHP applications by discussing application configuration, data storage, logging and error handling. We will cover some of the basic considerations when approaching these issues and evaluate our options in regard to implementation, efficiency and maintainability.


    With any application, there is a need to centralize certain information. Things like database credentials, file paths, constants, and initialization variables and options should be isolated from working code and managed in as centralized a way as possible. This allows you to alter these values in a centralized location rather than across any number of files.

    Centralizing Your Configuration 

    The need for centralized configuration is critical in any application, and is quite possibly the single strongest contributing factor to code maintainability. There are a number of approaches to this issue; it presents a particular problem when evaluating flexibility versus efficiency. The most typical methods for storing configuration files are with standard PHP files, XML files and INI files. Examples of a configuration file that contains only database credentials would look like the following.

     PHP
     <?php
     $settings[‘db’][‘user’] = ‘root’;
     $settings[‘db’][‘pass’] = ‘’;
     $settings[‘db’][‘host’] = ‘localhost’;
     $settings[‘db’][‘name’] = ‘test’;
     ?>

     INI
     ; settings
     [db]
     user = root
     pass =
     host = localhost
     name = test

     XML
     <settings>
      <db>
       <user>root</user>
       <pass></pass>
       <host>localhost</host>
       <name>test</name>
      </db>
     </settings>

    These files are fairly straightforward, each containing a username, password, hostname and default database. Implementing the PHP file is simple enough – simply include it with the “require_once” function and no further processing is required, the “$settings” array is ready to use. The PHP version is also the least maintainable, in that it looks just like any other code and is not especially easy to read.

    Using the INI file is not quite as simple, but requires only using the “parse_ini” function to retrieve the values from the file into an array. The INI file is much easier to read than the PHP version, but it could still be better. The XML version is by far the slowest, requiring at least some string matching and at most parsing the entire file. The tradeoff is evident in the semantic, easy to read nature of XML. XML files are also easily managed by a large number of programs, require the least explanation for other developers to implement, and offer the greatest portability to other modern scripting languages. This means that configuration could be shared by Java, Python, .NET, or Perl applications, for example, with ease.

    Realistically, the processing time between each of these files is almost entirely negligible. However, as configuration files grow to store other information, that changes. For the most scalability and raw speed, using a PHP configuration file is the best bet, but I personally advocate the use of XML. The tradeoff for maintainability is worth it so long as you minimize the amount of XML parsed on every script page.

    This is simply my opinion, and any of these options will do. In addition to using a variable to hold system settings, you have the option of storing settings in constants. Deciding what data should be stored as a constant and what data should be stored in a system variable is mostly a matter of preference, though I prefer to store system configuration in a settings array or object, and system messages and frequently used values (such as the number of seconds in a day or the maximum dimensions of uploaded images) in constants. The following list contains some common information to store in configuration files.

    1. Database credentials
    2. File paths
    3. System strings and error messages
    4. Constants for frequently used system values

    Centralizing important system information is critical to the maintainability of any application and is invaluable when handing off a project to a new developer, because understanding how system data is managed is one of the first steps in understanding an unfamiliar application.



     
     
    >>> More PHP Articles          >>> More By David Fells
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek