Main Content

PHP5 lets you say no

Archive - Originally posted on "The Horse's Mouth" - 2005-02-07 03:45:40 - Graham Ellis

The decision to release "PHP5" rather than a "PHP4.4" was taken due to a specific change in the object model - in PHP4 the code
            $second = $first;
would clone an object (duplicate all its contents, whereas in the new model just the reference will be copied, so giving the same object a second name. This change will "break" a few applications that made use of the old (and arguably incorrect) model, and was the incompatability that forced the number change. But with the change in model number, many many other things have been added in as well as "now is a good time to do this".

Many compatible enhancements have been made to PHP4's Object oriented model, and the theme of many of them is to allow the class developer to say NO to the application programmer. From its "Personal Home Pages" origins, PHP has been a trusting language but progressively options have been added to allow for trust to be restricted. The sort of facilities that would have been ludicrous in the early days, but are necessary where a team of programmers (and sometimes a large team) is involved. Amongst the enhancements:

** The ability to label object variables and methods as Private and Protected. (You can use public too, but that's an alternative to the old var label)

** The abiity to mark something final so that it can't be overridden in a subclass.

** The ability to define a class as abstract so that the application programmer can't directly create an instance, and so that the writer of extended classes is forced to provide certain methods.

** The ability to define an interface to that a class can be forced / checked to contain certain methods at compile time.

Other enhancements in the PHP5 model (which do extend the language) include the ability to define a destructor method to be run when an object is no longer required (or at some time thereafter). The descructor method should be called __destruct. You may also name your constructor __construct now, and if you're coding only for PHP5 it's recommended that you do so to prevent the class name appearing within the class. See "Objects in PHP" for links to examples of OO in PHP - both versions 4 and 5.