In PHP 5 the object implementation was rewritten and new capabilities introduced. The
final keyword was introduced to prevent defining classes using inheritance
(extends keyword). This can be important from a security perspective and to give
clarity when publishing classes that are not intended to be overridden. The
__construct() method is the new convention for defining constructors.
Visibility of methods can be declared using the keywords public,
protected, and private (similar meanings to Java). Using the minimum appropriate
visibility is a useful tool in scaling complex projects and a good practice to use from API
publishing and security perspectives. The keyword var in the example above is a PHP 4 convention
and equivalent to public in PHP 5. Method declarations without a visibility modifier are
treated as public. The following program demonstrates some of these concepts using a modified
version of the PHP 4 class above.
The program additionaly demonstrates the use of the interface keyword. An
interface defines the signature (or interface) of class. The
class then implements the interface. This gives calling code a confidence that the
methods declared in the interface will be there without having to worry about the details
of the implementation. However, their is arguably limited reason for interfaces in a
scripting language like PHP.
The class also demonstrates the use of the __toString() function. This
is a standard function that determines how an object of that class type will be stringified.
It can be convenient for logging purposes to keep the calling code cleaner. The program
output is the same as the PHP 4 version.
The static keyword was also introduced in PHP 5. It has a similar meaning to Java.
The const keyword (for constant) was also introduced in PHP 5 to define symbolic
names for values that do not change.
The abstract keyword was introduced in PHP 5 to allow for definition of class interfaces
without allowing instantiated. Abstract classes must contain at least one method that is
marked abstract. These abstract methods must be overridden in a derived class
for it to be able to be instantiated.
An additional keyword introduced in PHP 5 is the final class modifier. Use
final when it does not make sense to override a class. Here is a script that
demonstrates some of these concepts.
The abstract class Symbol defines two methods, getName() and
getAnnotation(), which child classes must implement. The child class
Adenine extends Symbol and declares itself as final
to indicate that it should not be extended itself.
The class Adenine
declares two constants using the const keyword. These constants are
accessed from inside the class with the self keyword to indicate the class
(as opposed to this instance) itself and the :: operator
to indicate a static context. To prevent calling code from instantiating the
Adenine class the constructor is declared as private. To access the
one and only (singleton) instance of the class calling code uses the static
method getInstance(). Static fields and methods are associated with the
class definition rather than instance objects. The output of the script is
In PHP 4 all variables were copied by value when the = operator is used. However,
in PHP 5 objects are copied by reference. This means that if the copy is changed the original
is changed because actually the copy is a pointer to the original. In PHP 4 you need to use
the =& to copy by reference. The clone keyword can be used to make
a copy of an object in PHP 5.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.