PHP
  Home arrow PHP arrow Page 3 - Writing Clean and Efficient PHP Code
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? 
PHP

Writing Clean and Efficient PHP Code
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 139
    2004-05-26


    Table of Contents:
  • Writing Clean and Efficient PHP Code
  • Loops
  • Objects
  • Code Reusability

  • 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


    Writing Clean and Efficient PHP Code - Objects
    ( Page 3 of 4 )

    Objects can cripple your page load time in PHP. PHP is still a young language and with version 5 will finally support "real" Objects, and supposedly offer faster compilation of objects. This is a good thing, as there is little room for a non-object-oriented application in the real world. When you are assembling objects in code, the natural thing to do is to simply copy objects as needed. For example, say every object in your application requires a database object to populate itself and save it's state. However, since PHP is middle-ware and there should never be concurrent database access by a single PHP page, it makes sense to have your objects share a single database object. This point spans a vast array of situations where objects need to be shared and passed around. C++ programmers understand this concept, as C++ has wretched garbage collection. Fortunately, PHP does not share this shortcoming, but poor memory use within an application page can still result in pathetic load times and a crippled server if there are a lot of concurrent users and a lot of objects being copied haphazardly.

    The point of this section is this: carefully consider the way in which you utilize objects in your PHP code. It is perfectly fine to have PHP that is completely object oriented and has nothing more than the equivalent of a Main() in a C++ app, but you must consider how your objects will be used, how large the contents of those objects will be, and how they will share those contents and methods.

    Best Practices

    This section could be made an entire book on it's own, and often has. It is subject to much debate and a variety of standards. As with any language, some best practices can be applied to that language specifically. An example that truly stands out is using single quotes (') vs using double quotes ("). The PHP parser treats these two token identifiers very different. PHP treats anything inside a matching pair of single quotes as a string literal and does not parse the contents of that string. However, PHP treats anything inside a matching pair of double quotes as string data that may contain references to instance variables, objects, or functions, and will parse the contents of the string. This can result in major performance disparities in a page with extensive data output. I ran a quick test printing 1,000 lines of output in the following two methods:

    // OPTION 1
    print "The value of variable A is $a. The value of variable B is $b. The value of variable C is $c.
    ";

    // OPTION 2
    print 'The value of variable A is '.$a.'. The value of variable B is '.$b.'. The value of variable C is '.$c.'.
    ';

    Option 2 ran roughly thirty percent faster on my server. Results will vary per server and page size. The big difference between these two examples is that in Option 1, PHP is required to full parse the contents of the string, doing printf-style substitution for instance variables. Option 2 is the equivalent of concatenating seven string literal values together and printing them out, which requires no actual string parsing, just some in-memory operations and a call to stdout.

    Other "best practices" that are more thoroughly documented include:

    • Using a consistent and logical naming convention for objects, functions, classes, etc.
    • Commenting code line by line (or as near to line by line as is needed to clearly explain what operations are being performed).
    • Indenting code to reflect the beginning and ending of statements (particularly statements enclosed in curly braces).
    • Organizing files in a logical directory structure with easy to understand directory and file names.
    • Making code as modular and reusable as possible.

    These things may seem obvious to seasoned programmers, but for a lot of beginners, these are things that are often overlooked in haste. All of these best-practices contribute to code reusability, readability, and maintainability, which is the topic of the next page.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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