Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is that valid to use private static callbacks in PHP?

Tags:

php

This works:

class MyClass {
  private static $data = array( 'banana', 'cherry', 'apple' );

  private static function sort_by_text( $first, $second ) {
    return strcasecmp( $first, $second );
  } 

  public static function sorted_data() {
    usort( self::$data, array( __CLASS__, 'sort_by_text' ) );
    return self::$data;
  }
}

print_r( MyClass::sorted_data() ); 
// Array ( [0] => apple [1] => banana [2] => cherry ) 

But, PHP docs always use public callbacks.

Is the fact that callbacks can be private just not documented well, or making them private can lead to issues?

like image 578
Misha Moroshko Avatar asked Jun 18 '13 02:06

Misha Moroshko


1 Answers

Callbacks are context aware and you can see that there are some bugs around it, like: https://bugs.php.net/bug.php?id=62547

https://bugs.php.net/bug.php?id=63468

But it is being fixed and therefore is supported :)

like image 181
Bond Avatar answered Nov 11 '22 16:11

Bond