Article

PHP5: Coming Soon to a Webserver Near You

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Next

Interfaces

Interfaces are a new feature of PHP5, which were possible for disciplined developers using PHP4, but only as a loose convention.

Interfaces are essentially a "contract" which any concrete class that implements the interface must obey. The advantage of having interfaces is that they allow developers to state their intentions within a library of classes, and so help other developers who add their own code to conform to the standards of the library. Interfaces are also useful when need to identify an object by introspection.

Introspection refers to the ability of your code to examine data at runtime and determine the type, methods available (if it's an object), and so on. PHP already provides a number of introspection functions, such as is_a and a new keyword with PHP5, "instanceof", which does effectively the same thing as is_a() but should improve readability. Introspection may receive many more features by the time PHP5 is stable.

An interface class simply declares a set of method names without the methods having a body. For example:

     
<?php      
interface Iterator {      
   function fetch();      
   function howMany();      
}      
     
class Presidents implements Iterator {      
   private $presidents = array();      
   function __construct() {      
       $this->presidents = array (      
           'Jimmy Carter','Ronald Reagan','George Bush',      
           'Bill Clinton','George W. Bush'      
       );      
   }      
   function fetch() {      
       $president = each($this->presidents);      
       if ( $president ) {      
           return $president['value'];      
       } else {      
           reset ( $this->presidents );      
           return false;      
       }      
   }      
     
   # Commented out to produce error      
   /*      
   function howMany() {      
       return count($this->presidents);      
   }      
   */      
}      
     
#Fatal Error - Presidents does not implement the howMany() method in Iterator!      
$presidents = new Presidents();      
     
while ( $president = $presidents->fetch() ) {      
   echo ( $president.'<br />' );      
}      
?>      

Script: interface.php

In the above example, the Iterator interface defines two methods that classes implementing it must provide. It's down to the implementing class to provide the logic for the methods. Because I commented out the howMany() method in the Presidents class, when I instantiate it PHP gives me a fatal error reminding me I need to make the howMany() method available again.

Note: regarding Iterators, see "That's not all folks..." later in this article.

While PHP5 was evolving, there was some argument over multiple inheritance (as is the case with C++) vs. interfaces (the Java way). Multiple inheritance (the extension of multiple parent classes) is perhaps more powerful, but it also introduces a lot more complexity to a language and (arguably) often results in bad design. The PHP group chose the Java approach, which may not please everyone, but is certainly sensible.

Personally, I think interfaces are the better way to go; PHP needs more focus on standardization rather than choice (of which it already offers a lot). At the same time, questions of performance will likely raise their heads where interfaces are concerned, as PHP is interpreted rather than compiled.

For more depth on Interfaces vs. Multiple Inheritance try this discussion and the article it refers to.

Overloading

Overloading objects in PHP4 became possible via the (experimental) overload extension, which you can find explained in more detail here. With PHP5, overloading has become a central feature of the language, and will no doubt prove important when dealing with Web services as well as integration with other platforms like Java and .NET.

Overloading, in a PHP context, is not the same as method overloading in (statically typed) languages like C++ and Java. PHP provides the function func_num_args(), which can be combined with type introspection like is_int() should you need to achieve the same effect. In practice though, as PHP is dynamically typed, you rarely need Java/C++ style method overloading.

In PHP, overloading refers to the ability to call methods and object properties which have not been explicitly declared within the class. PHP allows the use of three "magic" methods __get(), __set() (both of which apply to object properties) and __call() (which applies to object methods) within which you can place code for dealing an "unexpected" call.

Here's an example using the __call() method in PHP5 to allow native PHP functions to be called via an object:

     
<?php      
class MyString {      
   private $string;      
   function __construct($string) {      
       $this->string = $string;      
   }      
   function __call($method,$params) {      
       switch ($method) {      
           case'strlen':      
               return strlen($this->string);      
           break;      
           case'substr':      
               return substr($this->string,$params[0],$params[1]);      
           break;      
           default:      
               return false;      
           break;      
       }      
   }      
}      
     
$myString = new MyString('Hello World!');      
     
echo ( 'The string length is '.$myString->strlen().'<br />' );      
     
echo ( 'Substr: '.$myString->substr(3,6).'<br />' );      
?>      

Script: overload.php

The native PHP functions substr() and strlen() are now callable via an instance of the MyString class, without which you need to explicitly declare them as methods.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links