HomePHP Page 3 - Writing Clean and Efficient PHP Code
Objects - PHP
If you've ever had to go back to an application you wrote after an extended period of time, you already know the value of clean, well documented and efficient code. But how can you make your code better? Here are some tips that'll help you speed up and clean up your development cycle.
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.
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.