note 94312 added to intro.spl-types
| From: | Tom at osu1 dot php dot net | Date: | Wed, 28 Oct 2009 16:34:57 +0000 |
| Subject: | note 94312 added to intro.spl-types | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162196@lists.php.net to get a copy of this message | ||
For your information, here is another work-around for this issue, that even works with PHP4 (in case
you need to be compatible):
/**
* @param string $foo
*/
function foo($foo)
{
assert('is_string($foo); // Invalid argument type');
// do some foo
}
This single-line of code should fix it for most people. It will throw an error whenever the
expression evaluates to false (if $foo is not a string).
Most errors concerning scalar types are found during development or unit-testing. PHPUnit is fine
with these and reports failed assertions as failed tests, also presenting the included message.
Note that assertions need to be turned on for this to work. See the manual page on assertions for
more details.
Note that you may deactivate the checks on the target machine, so this comes without any performance
penalty. So don't hesitate to make extensive use of this PHP-feature.
Note though: DON'T RELY ON ASSERTIONS FOR CHECKING USER INPUT!
In that case you may want to try this code:
is_string($foo) || trigger_error('String expected', E_USER_ERROR);
Or: simply throw an exception (as usual).
----
Server IP: 212.124.37.9
Probable Submitter: 91.35.160.22
----
Manual Page -- http://www.php.net/manual/en/intro.spl-types.php
Edit -- https://master.php.net/note/edit/94312
Del: integrated -- https://master.php.net/note/delete/94312/integrated
Del: useless -- https://master.php.net/note/delete/94312/useless
Del: bad code -- https://master.php.net/note/delete/94312/bad+code
Del: spam -- https://master.php.net/note/delete/94312/spam
Del: non-english -- https://master.php.net/note/delete/94312/non-english
Del: in docs -- https://master.php.net/note/delete/94312/in+docs
Del: other reasons-- https://master.php.net/note/delete/94312
Reject -- https://master.php.net/note/reject/94312
Search -- https://master.php.net/manage/user-notes.php