note 102218 added to class.invalidargumentexception
| From: | Joey at anti-culture dot net | Date: | Thu, 03 Feb 2011 05:08:01 +0000 |
| Subject: | note 102218 added to class.invalidargumentexception | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176613@lists.php.net to get a copy of this message | ||
In my opinion this exception is invaluable for validating arguments- for example providing strict
typing a la C:
<?php
function tripleInteger($int)
{
if(!is_int($int))
throw new InvalidArgumentException('tripleInteger function only accepts integers. Input
was: '.$int);
return $int * 3;
}
$x = tripleInteger(4); //$x == 12
$x = tripleInteger(2.5); //exception will be thrown as 2.5 is a float
$x = tripleInteger('foo'); //exception will be thrown as 'foo' is a string
$x = tripleInteger('4'); //exception will throw as '4' is also a string
?>
----
Server IP: 69.147.83.197
Probable Submitter: 88.104.118.11
----
Manual Page -- http://www.php.net/manual/en/class.invalidargumentexception.php
Edit -- https://master.php.net/note/edit/102218
Del: integrated -- https://master.php.net/note/delete/102218/integrated
Del: useless -- https://master.php.net/note/delete/102218/useless
Del: bad code -- https://master.php.net/note/delete/102218/bad+code
Del: spam -- https://master.php.net/note/delete/102218/spam
Del: non-english -- https://master.php.net/note/delete/102218/non-english
Del: in docs -- https://master.php.net/note/delete/102218/in+docs
Del: other reasons-- https://master.php.net/note/delete/102218
Reject -- https://master.php.net/note/reject/102218
Search -- https://master.php.net/manage/user-notes.php