It is important to understand that PHP 5 made great leaps toward becoming an object-oriented language with greater support for classes and error handling. This Short Cut uses PHP 5 for all the examples. However, each example can be converted to PHP 4 with some effort, which will not be covered in-depth here.
There are several differences between the two versions of the language to keep in mind. If you feel that you already have a firm grasp on the differences, skip this section.
As of PHP 5, you can hint to the interpreter what type of object you expect to be passed to your function or method (a function inside a class). The hint can be either a class name or the reserved "array" word. For example, if we want a function to accept only arrays it is as simple as having the function signature:
function Foo(array $x)
Likewise if we have a class Bar, we can use the signature:
function Foo(Bar $x)
This will ensure that only Bar is passed to the function.
In PHP 4, mimicking this ability is rather simple, however, not as versatile:
function Foo($v) { if ( !is_array($x) ) { echo "Parameter must be an array"; return; } ...
Without the type hinting there isn't any convenient way to tell if an object is a specific type of object.
Exception handling is a new and valued addition to PHP 5. This is one of the major new features of PHP 5. Exceptions are a way of catching and handling errors that may occur in our scripts. In PHP 5 it is virtually identical to those in C++ or Java. The syntax probably looks familiar.
try { throw new Exception("Error message"); } catch ( Exception $e ) { echo $e->getMessage(); }
So what happens when designing a script to work with PHP 4? Many
of the examples will not work. For those of you who can't upgrade to PHP
5, there is a somewhat less eloquent solution: return
false;
.
One of the lesser known features of PHP is that it has two
strict comparison operators, ===
(three sequential equal signs) is "exactly equal to" and
!==
is "not exactly equal to," which will compare
both the type and value of a variable or constant. The biggest advantage
of this is that when using the strict comparison operators false will no
longer equal 0 or null
. Where possible you can return
false when an error occurs and then check if the result is identical to
false. If you want an even more complete solution you may store the
exception message in a global variable. The examples in this document
use only the PHP 5 exception method.
Another welcomed addition is the "magic" method
__toString()
. This method is automatically called
when attempting to access an object as a string. Unfortunately, PHP 4
does not have an equivalent of this method. Instead, the name of the
class and the unique ID associated with that object will be printed.
This restriction on PHP 4 is easily circumvented by calling the
__toString()
method manually within your script.
However, because most of the classes in this Short Cut are static, it
isn't used much.
The __toString()
method is used in several
sections of this text. To avoid confusion and to make the code easier to
read, it will be called manually. However, in PHP 5, the behavior would
be the same even if the object was references as a string without the
explicit call.
Most programmers who have used heavily object-oriented languages such as Java and C++ are familiar with the visibility keywords public, private, and protected. When PHP 4 was released, these keywords did not exist. PHP 5 removes this limitation. In the best interest of efficient team-oriented software engineering this text uses the visibility keywords for every member property and method. Unfortunately, this will cause headaches on PHP 4 servers.
The workaround is to use var
instead of the
visibility keyword for properties (variables) inside the class and
remove the visibility keyword altogether from method definitions. Most
methods and properties in here are public; however, there are a few
private methods that are also used.
Visibility is always defined prior to the method or parameter. An example of a class with two private members would be:
class Foo { private $bar; private function getBar() { return $bar; } }
If you're unfamiliar with visibility, you can use Table 3 to determine which type of visibility to use.
Table 3. Using visibility
Keyword | When to use |
---|---|
| When a member must be accessible everywhere. Including by the current class as well as all other classes and in the global scope. |
| When a member should only be accessible to the current class and no other. Even inherited classes cannot see the private member. |
| When a member must be inaccessible from other classes or the global scope but should still be accessible from classes that inherit the current class. |
Static members behave much like static members in Java. They can be accessed without the need to initialize an object of the class. In fact, static is the most used keyword in the Short Cut for that reason. This can be useful for creating utility classes.
Static methods are called via the scope resolution operator (a double colon). In this respect, a completely static class in PHP is similar to a namespace in C++.
Unfortunately, there isn't any equivalent in PHP 4. To solve this problem, if you choose to implement your system in version 4 you must either create the utility functions outside of the scope of a class or create a global instance of the class.
Prior to PHP 5.2, JSON parsing support in PHP was provided by third-party scripts and extensions. In the new versions of the PHP 5.0 branch, this functionality is included in an extension that is bundled and enabled by default on all PHP installation. This extension is a prime example of a feature that provides significant performance increases and cost reduction, which is not as readily available in PHP 4 as it is in 5.
Another time saving feature of PHP 5 is the new SimpleXML class. This Short Cut covers parsing and generating XML using both simple XML and the more traditional methods. Depending on your preference you may find SimpleXML a much appealing solution.
If so many problems arise from using PHP 5, then why use Version 5 at all? Why not just stay with PHP 4? There are a few reasons for this. One is that the PHP 5 branch—and soon PHP 6—contain all major functionality upgrades to the language. Although the PHP 4 branch is still updated when bug fixes and security enhancements are needed, the base functionality of PHP 4 is not likely to change. If PHP 5 has significantly more functionality than PHP 4, as demonstrated earlier, then it seems wise to implement this functionality rather than waste time recreating features.
Version 5's largest advancements come in the form of its new object-oriented features. This is good news for Ajax developers. It allows for greater reusability in code and easier interoperability between software written by numerous programmers. It provides a standard method of performing tasks that were previously accomplished via often messy hacks (particularly in error handling). Arguably, most importantly, it makes the code more readable by removing some ambiguity and making explicit declarations of the intent of a method or property.
The bottom line is that PHP 5 programs are less expensive to develop by reducing common problems and encouraging structured design. Whereas previous versions tended to inadvertently encourage ad hoc design.
The biggest issue with PHP 5 is that PHP 5 scripts do not always gracefully degrade to run on a PHP 4 server. With all that said, you may still opt to stay with PHP 4 either by necessity or preference. If you do so, hopefully this Appendix gave you enough information to help you begin to convert the scripts included in this text into PHP 4 compatible scripts.
Ultimately, which version you decide to use depends upon individual project managers. There isn't any one-size-fits-all solution.
98.82.120.188