Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 10th, 2012  |  syntax: PHP  |  size: 1.16 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. class Test
  3. {
  4. public function index( )
  5. {
  6.     $arr = array(
  7.         array('a' => 'aap', 'n' => 'noot', 'm' => 'mies'),
  8.         array('a' => 'ding', 'b' => 'flof', 'c' => 'bips'),
  9.         array( 'd' => 'do', 'e' => 're', 'c' => 'mi')
  10.     );
  11.  
  12.     $func = array( $this, '_user_func' );
  13.  
  14.     foreach ($arr as $key => &$value) {
  15.         //$this->_do_callback($func, $value, $key); // No exception but array not modified afterwards
  16.                 $param = array(&$value);
  17.         call_user_func( $func, $param, $key ); // Exception: Parameter 1 to TestRef::user_func() expected to be a reference, value given
  18.     }
  19.         unset($value);
  20.     var_dump($arr);
  21. }
  22.  
  23. private function _do_callback( $func, array &$row, $row_id )
  24. {
  25.     if ( is_callable( $func ) )
  26.     {
  27.         return call_user_func( $func, $row, $row_id );
  28.     }
  29.     else
  30.     {
  31.         throw new Exception( "Error doing callback. Callback empty or not a callable function." );
  32.     }
  33. }
  34.  
  35. private function _user_func( $_arr, $index )
  36. {
  37.         $arr = &$_arr[0];
  38.     foreach ($arr as $key => $value) {
  39.         $arr[$key] = 'replaced';
  40.     }
  41.     //var_dump($arr); // Works!
  42. }
  43. }
  44.  
  45. $test = new Test;
  46.  
  47. $test->index();
clone this paste RAW Paste Data