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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC