Type hinting

From: Date: Sat, 08 Dec 2007 21:38:23 +0000
Subject: Type hinting
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14  Groups: php.internals 
Request: Send a blank email to internals+get-33875@lists.php.net to get a copy of this message
A few weeks ago I wrote a message on this list about my patch for scalar type hinting. I've been using it for about a month now in a large scale application im developing with no problems. It allows type hinting for the following types: int, float, string, bool (boolean), num (int or float), scalar (int, float, string, or bool), object, and resource. All of these types have been very useful. Num has been useful for any mathematical functions, scalar has been the most useful, mostly for echoing values and interacting with the database (as a database cant store arrays or objects). The type hinting is all 100% optional and has caused no problems for me at all, I use it in most function but some functions I'll use it for one parameter and not the other, or none at all with no problems. What is the general opinion on this? Examples below: <?php function multiply(num $a, num b) { return $a * $b ; } multiply(5, 1) ; // works multiply(3.7, 8.2) // works multiply("foo", 5) ; // Catchable fatal error: Argument 1 passed to multiply() must be a number, string given function displayStuff(scalar $stuff) { echo $stuff ; } displayStuff('Hello!') ; // works displayStuff(123) ; // works displayStuff(array('a' => 'b')) ; // Catchable fatal error: Argument 1 passed to displayStuff() must be a scalar, array given ?>

« previous php.internals (#33875) next »