8000 Type inconsistency between the filter_var function and it's callback on the $value parameter. · Issue #4726 · php/doc-en · GitHub
[go: up one dir, main page]

Skip to content
Type inconsistency between the filter_var function and it's callback on the $value parameter. #4726
@tonurics

Description

@tonurics

Description

The filter_var function has the following description:
filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed

The "callback signature" [description] of the User Defined Filter of the FILTER_CALLBACK filter is:
callback(string $value): mixed

Problem:

This inconsistency prevents FILTER_CALLBACK from being used to sanitize arrays, such as those set in the predefined variables after an HTTP POST request.

e.g.: The following code generates a type error. [EDIT 2025-06-09:] The code sample in this post does not demonstrate the reported problem, see updated sample here: #4726

<?php
unset($json);
$json=filter_var(
	value:$_POST['array'],
	filter:FILTER_CALLBACK,
	options:[
		'options'=>function(
			mixed $value,
		):mixed{
			return json_encode($value);
		},
	],
);

Solution:

The type of the $value parameter in the "callback signature" [description] should be changed to match its filter_var calling function. i.e.:
callback(mixed $value): mixed

PHP Version

PHP 8.4.7 (cli) (built: May  9 2025 07:02:39) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.7, Copyright (c), by Zend Technologies

Operating System

Debian 6.1.140-1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Extension: filterbugDocumentation contains incorrect information

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0