Re: type hinting

From: Date: Fri, 04 Jan 2008 17:51:41 +0000
Subject: Re: type hinting
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-34429@lists.php.net to get a copy of this message
On Fri, Jan 04, 2008 at 12:37:19PM -0500, Robert Cummings wrote: > IMHO, optionally inclusion of type hinting for functions/methods can > only be a boon to code quality and readability. IMHO when a type hint is > provided and a parameter doesn't match the type hint then I think a > fatal error should occur. This forces the user of the function that has > type hinting to ensure their data is of the correct type. This prevents > accidental wrong data conversion. However, I see the other side of the > coin too where automatic type conversion could be desirable also. > Perhaps a mixed solution would be viable? > > <?php > > function foo( require int $a, require string $b ){} > > foo( '5', 'bleh' ); // <-- fatal error No. > ?> > > Contrast versus: > > <?php > > function foo( int $a, string $b ){} > > foo( '5', 'bleh' ); // <-- no exception or error $a in foo() will > // be type int (automatic conversion) Yes. If $a is '5' but reject if $a is '5five'. > ?> > > Versus (still allowed default style): > > <?php > > function foo( $a, $b ){} > > foo( '5', 'bleh' ); // <-- no exceptions or type conversions > > ?> > > Thoughts? > > Cheers, > Rob. > -- > ........................................................... > SwarmBuy.com - http://www.swarmbuy.com > > Leveraging the buying power of the masses! > ........................................................... -- Alain Williams Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php Chairman of UKUUG: http://www.ukuug.org/ #include <std_disclaimer.h>

« previous php.internals (#34429) next »