| 1 | <?php |
| 2 | |
| 3 | if ( !function_exists( 'get_called_class' ) ) { |
| 4 | function get_called_class( $bt = false, $l = 1 ) |
| 5 | { |
| 6 | if ( !$bt ) $bt = debug_backtrace(); |
| 7 | if ( !isset( $bt[$l] ) ) throw new Exception( "Cannot find called class -> stack level too deep." ); |
| 8 | if ( !isset( $bt[$l]['type'] ) ) { |
| 9 | throw new Exception ( 'type not set' ); |
| 10 | } else switch ( $bt[$l]['type'] ) { |
| 11 | case '::': |
| 12 | $lines = file( $bt[$l]['file'] ); |
| 13 | $i = 0; |
| 14 | $callerLine = ''; |
| 15 | do { |
| 16 | $i++; |
| 17 | $callerLine = $lines[$bt[$l]['line'] - $i] . $callerLine; |
| 18 | } while ( stripos( $callerLine, $bt[$l]['function'] ) === false |
| 19 | ); |
| 20 | preg_match( '/([a-zA-Z0-9\_]+)::' . $bt[$l]['function'] . '/', |
| 21 | $callerLine, |
| 22 | $matches ); |
| 23 | if ( !isset( $matches[1] ) ) { |
| 24 | // must be an edge case. |
| 25 | throw new Exception ( "Could not find caller class: originating method call is obscured." ); |
| 26 | } |
| 27 | switch ( $matches[1] ) { |
| 28 | case 'self': |
| 29 | case 'parent': |
| 30 | return get_called_class( $bt, $l + 1 ); |
| 31 | default: |
| 32 | return $matches[1]; |
| 33 | } |
| 34 | // won't get here. |
| 35 | case '->': switch ( $bt[$l]['function'] ) { |
| 36 | case '__get': |
| 37 | // edge case -> get class of calling object |
| 38 | if ( !is_object( $bt[$l]['object'] ) ) throw new |
| 39 | Exception ( "Edge case fail. __get called on non object."); |
| 40 | return get_class( $bt[$l]['object'] ); |
| 41 | default: return $bt[$l]['class']; |
| 42 | } |
| 43 | |
| 44 | default: throw new Exception ( "Unknown backtrace method type" ); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | ?> |
| 50 | No newline at end of file |