Re: RE: Optional scalar type hinting

From: Date: Thu, 03 Jan 2008 22:12:14 +0000
Subject: Re: RE: Optional scalar type hinting
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.internals 
Request: Send a blank email to internals+get-34364@lists.php.net to get a copy of this message
On 1/4/08, Stanislav Malyshev <stas@zend.com> wrote: > > There aren't two code models here at all. You can have a function > > parameter, or you can have a type-enforced function parameter. PHP > > So now to use such function you'd have to check your variables for > typing - otherwise your application blows up. And the type-checking > should be total - otherwise you miss some call to some function or some > code path bringing wrong value and your application blows up at runtime > - since static type checking is not available. Meaning, unless all of > your code is type-enforced, you'd have to write a wrapper around each > type-enforced function manually checking that wrong value didn't get in. > But you can do the same checks now, so what is the added value? type-hinting is asserting. checking of types is needed only on interface-border points (where type-hints would be used), but usually can be skipped in a lot points in "private methods" It's good to be sure, that input-value is exactly the one which is supposed to be there currently the typical code is: public function someMethod($var1) { if (!is_int($var1)) { throw new UnexpectedValueException(); } // … } it looks too long and (what is even worse) it hides actual "business-logic" behing checks. I prefer the following code: public function someMethod(integer $var1) { // … } -- Alexey Zakhlestin http://blog.milkfarmsoft.com/

« previous php.internals (#34364) next »