Re: Adding "numeric" type hint

From: Date: Wed, 29 Apr 2015 00:33:38 +0000
Subject: Re: Adding "numeric" type hint
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-86007@lists.php.net to get a copy of this message
On Tue, Apr 28, 2015 at 6:00 PM, Stanislav Malyshev <smalyshev@gmail.com> wrote: > Hi! > > > External data can have any form of numbers. > > Current PHP can handle them as "string". However PHP7's type hint cannot > > handle numeric data well because it only has "int" and "float" hints. > > This is exactly what's wrong with strict typing in PHP. However, if you > use real numeric string and do not enable strict typing - it works just > fine. We don't need more types for this. > > > There are cases that programmer want/need to handle any numeric values. > > There also cases when programmer needs to handle Roman numbers, phone > numbers, valid XML strings and TCP/IP headers. But we should not add > types into the language for those. > > > To avoid this problem, users must use "string" type hint and have to > > validate > > parameter by themselves. This ruins benefits of type hint. Most PHP will > not > > Typing is not solution for every data restriction, especially not in > PHP. If you app needs strings that can not be represented by PHP basic > types, you need custom validation code. > > > but "numeric" type hint may do the job. One function with > > "int"/"float" > > type hint > > could break app with current type hint implementation, though. i.e. > Working > > If you type your parameter, then you declare "I want this to fail if the > parameter is not of this type". Then you can't complain when it fails - > that's exactly what you asked for. > I agree with Stanislav here, if you want to accept any type of number, its easy enough to add your own checking to do that with the wonderful is_numeric. And for simplicity, make an invalidArgument method that you can call after manually checking if arguments are wrong: *http://3v4l.org/r0qO0 <http://3v4l.org/r0qO0>* Works for all versions this tool runs as well. -Ryan > > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.internals (#86007) next »