|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-05-01 11:40 UTC] peter dot schultz at classmarkets dot com
Description: ------------ --- From manual page: http://www.php.net/filter.filters.validate --- FILTER_VALIDATE_BOOLEAN with the FILTER_NULL_ON_FAILURE flag return false if the first argument is an object. According to the documentation it should be null. Test script: --------------- <?php var_dump(filter_var(new \StdClass(), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));' Expected result: ---------------- NULL Actual result: -------------- bool(false) PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 14:00:01 2025 UTC |
Hi there, Has this bugfix actually landed? If so, in which version, can't find it in the changelog. I'm currently testing on PHP 5.6.11-1ubuntu3.1 and I can still reproduce the problem. Here is my test case: <?php class x {} class y { public $m = 'n'; function doStuff() { $this->m = 'l'; } } $value = 'x'; var_dump(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // expected: NULL – works as expected $value = new stdClass(); var_dump(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // expected: NULL – returns false $value = new x(); var_dump(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // expected: NULL – returns false $value = new y(); var_dump(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // expected: NULL – returns false var_dump((string)$value); This is the output I get: NULL bool(false) bool(false) bool(false) PHP Catchable fatal error: Object of class y could not be converted to string in /home/benedict/Repositories/wellid/testcase.php on line 30 PHP Stack trace: PHP 1. {main}() /home/benedict/Repositories/wellid/testcase.php:0 benedict@minerva:~/Repositories/wellid$ vim testcase.php benedict@minerva:~/Repositories/wellid$ php -f testcase.php NULL bool(false) bool(false) bool(false) PHP Catchable fatal error: Object of class y could not be converted to string in /home/benedict/Repositories/wellid/testcase.php on line 30 PHP Stack trace: PHP 1. {main}() /home/benedict/Repositories/wellid/testcase.php:0 As you can see, none of these have a __toString()-method. Am I missing something?> is false, when it should be null according to the docs No. The filter_var() docs[1] state: | Note that scalar values are converted to string internally | before they are filtered. So basically you're calling: filter_var('', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) However, FILTER_VALIDATE_BOOLEAN is documented[2] to: | If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for | "0", "false", "off", "no", and "" […] So the behavior complies to the documentation. [1] <http://php.net/manual/en/function.filter-var.php> [2] <http://de2.php.net/manual/en/filter.filters.validate.php>