Advertisement
Guest User

Untitled

a guest
Sep 10th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement