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  
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

Writing Clean and Efficient PHP Code
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 138
    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:
      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


    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


       · I have been scripting with php 4, and always thought it was ok do just do what i...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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