note 94312 modified in intro.spl-types by danbrown
| From: | danbrown@php.net | Date: | Wed, 28 Oct 2009 16:36:55 +0000 |
| Subject: | note 94312 modified in intro.spl-types by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162197@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):
<?php
/**
* @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).
--was--
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).
http://php.net/manual/en/intro.spl-types.php