Sharp House

  • Increase font size
  • Default font size
  • Decrease font size

Strongly typing in PHP

Since when I started programming with PHP 3, it has always been a weakly typed programming language.

This means, in a nutshell, that you can declare a variable and assign it any value, letting the interpreter guess what type it is at runtime.

This has both advantages and disadvantages: it lets you build simple applications very quickly, switch variables' type at will during runtime, use different input types the same way (just think of the string '15.6' and the corresponding float value 15.6, PHP will add any number to either correctly, no explicit conversion required), and the list goes on.

Strongly typing, on the other hand, allows better management of large projects, as any additional check to find that last sneaky bug is always welcome, and comes with a number of different features that PHP is missing, or not implementing completely, namely functions polymorphism.
This implicates that it's not possible to have 2 functions with the same name and different type parameters, changing the actual function called based on the types of the parameters we give it.

Does this mean that it's not possible to use the best of the 2 worlds when we want to? The answer is yes and no - and I'm not talking about the tons of different workarounds that you'll find around the web, made to try and implement languages' abilities that PHP is missing.

Take a look at this code:

// this is the class we'll use as type
class StrongType {
 
  private $AnyTypeHolder;
 
  // classic PHP function accepting any type as parameter
  public function AddValue($anyType) {
    $this->AnyTypeHolder = $anyType;
  }
 
  public function ReadValue(){
    return $this->AnyTypeHolder;
  }
 
}
 
class TypeTest {
 
  // unless we change type inside this class, this will be always be
  // a StrongType object
  private $strongTypeInstance;
 
  // this accepts StrongType objects *only*, throwing an exception
  // if we try to assign anything else (string, int, a different class...)
  // note the StrongType before $typedObj
  public function __construct(StrongType $typedObj) {
    $this->strongTypeInstance = $typedObj;
  }
 
  public function ReturnStrongType() {
    return $this->strongTypeInstance;
  }
 
}
 
$strongType = new StrongType();
$strongType->AddValue(99);
 
$weakType = 'A string';
 
// works
$typeTestFirstObj   = new TypeTest($strongType);
// throws an exception
$typeTestSecondObj  = new TypeTest($weakType);

I bet that most of you reading never seen a type in a function declaration in PHP. Neither did I, until a few days ago.
The surprising thing is - it actually works. It is not strong typing, it's called type hinting here.
I tested it with both PHP 5.2.6 and 5.2.5, and both behave just like I wrote in the comments. The documentation about this feature is not quite easy to find nor common knowledge, but does indeed exist in the official documentation (and just there seems, didn't really find any other articles about it).

I don't think that this feature will change much when it comes to PHP coding as strong typing would, for a few reasons.
First, basic types such as string and int aren't objects, so they can't be used to apply this "trick", and it seems that it's something that can be done just in the function parameters declaration, not properties.
You'd have to make your own Int, String etc class, just like .net does, to have true type safety, at least in function calling.
Also, this seems to be limited to parameters, so no typed class properties.
It is certainly something that is going to be useful, and hopefully expanded upon - we could use the best of both strong and weakly typed languages, but not yet.

 
« November 2008 »
Mo Tu We Th Fr Sa Su
          1 2
3 4 5 6 7 8 9
10 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30