php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15694 array_filter does not work withing classes
Submitted: 2002-02-23 19:56 UTC Modified: 2002-02-23 20:07 UTC
From: ribrdb at yahoo dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.1.1 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ribrdb at yahoo dot com
New email:
PHP Version: OS:

 

 [2002-02-23 19:56 UTC] ribrdb at yahoo dot com
There is no way to safely use array filter from within a class.  Using class::functionname should work for the callback function as in:
<?php
class test {
  function filter($var) {
    return preg_match('/test/',$var);
  }
}
$array["test"]="This is a test";
$array["not"]="This shouldn't be here";
$array = array_filter($array, "test::filter");
print_r($array);
?>

but this just says:
Warning: array_filter() expects argument 2, 'test::filter', to be a valid callback in /usr/home/ribrdb/web/php/classtest.php on line 11


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-23 20:07 UTC] torben@php.net
From the manual (http://www.php.net/array_filter):

  Note: Instead of a function name, an array containing an   
  object reference and a method name can also be supplied.

So the correct code would be:

<?php
error_reporting(E_ALL);

class test {
    function filter($var) {
        return preg_match('/test/', $var);
    }
}

$test = new test;

$array["test"] = "This is a test";
$array["not"] = "This shouldn't be here";
$array = array_filter($array, array('test', 'filter'));
print_r($array);

?>


Torben

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 11:01:34 2025 UTC