I am getting the following message:

[07-May-2003 17:41:20] PHP Warning: array_filter() [<a href='http://www.php.net/function.array-filter'>function.array-filter</a>]: The second argument, 'odd', should be a valid callback in .../t.php on line 11

for the following code:

<?php
class asdf {
  function odd($var) {
    return ($var % 2 == 1);
  }

  function testthis() {
    $array1 = array(6,7,8,9,10,11,12);

return array_filter($array1, "[B]odd[/B]");
  }
}

$x = new asdf();
print_r($x->testthis());
?>

So how do you refer to a callback from within a class? I've tried using "asdf::odd", "this->odd", "$this->odd" as the callback parameter, but I get the same message in each case.

Anybody know??

Thanks.

    After working with my small example (see previous post) and implementing ahundiak's workaround, I moved this fix to the real class that I've been working with which is much larger and uses the $this reference extensively.

    I found that once you make a call to a class method using the :: operator or the array($myclass, $mymethod) syntax, any subsequent calls to class methods using the $this keyword result in:

    Call to a member function on a non-object

    <?php
    
    class asdf {
      function odd($var) {
        // can't do this once the :: or array($myclass, $mymethod) has been used!
        $var = $this->returnme($var);
        return (asdf::returnme($var) % 2 == 1);
      }
    
      function testthis() {
        $array1 = array(6,7,8,9,10,11,12);
    
    return array_filter($array1, array("asdf", "odd"));
      }
    
      function returnme($var) {
        return $var;
      }
    }
    
    $x = new asdf();
    print_r($x->testthis());
    
    ?>
    

    Any workarounds for this??

    • TIM

      Replace the class name with the object:
      return array_filter($array1, array($this, "odd"));

      After posting the below comments I took a walk around the lake and it occurred to me that if array_filter was smart enough to deal with a class name then it might be smart enough to deal with an object as well.

      The above change works on your sample. But you trying to do something a bit dangerous. And if the odd() method tries to make changes to the object then you may get unexpected results.

      So if you still have trouble then the following should work:

      You are using asdf_odd as a static function.

      There is no $this defined within static functions because there is no actual object created. Hence the error.

      To do what I think you are trying to do will require writing a wrapper function for array_filter to call.

      $asdf_odd_filter_object = null;  /* Just a holder for now */
      function asdf_odd_filter($var)
      {
        global $asdf_odd_filter_object;
      
        return $asdf_odd_filter_object->odd($var);
      }
      class asdf()
      {
      function testthis() 
      {
        global $asdf_odd_filter_object;
          $array1 = array(6,7,8,9,10,11,12);
      
        $asdf_odd_filter_object = $this;
      
      return array_filter($array1, 'asdf_odd_filter');
        }
      

      Remember that the array_filter function has no idea it is trying to call an object's method.

      Untested code.

        Write a Reply...