r109740 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109739‎ | r109740 | r109741 >
Date:15:23, 22 January 2012
Author:foxtrott
Status:deferred
Tags:
Comment:
fix bug 33832 ([SF] undefined function get_called_class() SF_FormInput.php on line 286)
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_FormInput.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/forminputs/SF_FormInput.php
@@ -283,7 +283,10 @@
284284 global $sfgFieldNum, $wgOut;
285285
286286 // create an input of the called class
287 - $calledClass = get_called_class();
 287+ // TODO: get_called_class was introduced in PHP 5.3. The call to
 288+ // SF_Utils::get_called_class() can be removed once support for PHP 5.2
 289+ // is dropped.
 290+ $calledClass = function_exists('get_called_class')?(get_called_class()):(SF_Utils::get_called_class());
288291 $input = new $calledClass ( $sfgFieldNum, $cur_value, $input_name, $is_disabled, $other_args );
289292
290293 // create calls to JS initialization and validation
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -819,4 +819,60 @@
820820 return $hasFactory ? SpecialPageFactory::getPage( $pageName ) : SpecialPage::getPage( $pageName );
821821 }
822822
823 -}
 823+ /**
 824+ * Compatibility function.
 825+ *
 826+ * This delivers the PHP 5.3 functionality also for 5.2
 827+ * Pretty ugly.
 828+ *
 829+ * Taken from http://djomla.blog.com/2011/02/16/php-versions-5-2-and-5-3-get_called_class/
 830+ *
 831+ * @param type $backtrace
 832+ * @param type $level
 833+ * @return type
 834+ */
 835+ function get_called_class( $backtrace = false, $level = 1 ) {
 836+ if ( !$backtrace )
 837+ $backtrace = debug_backtrace();
 838+ if ( !isset( $backtrace[$level] ) )
 839+ throw new Exception( "Cannot find called class -> stack level too deep." );
 840+ if ( !isset( $backtrace[$level]['type'] ) ) {
 841+ throw new Exception( 'type not set' );
 842+ }
 843+ else
 844+ switch ( $backtrace[$level]['type'] ) {
 845+ case '::':
 846+ $lines = file( $backtrace[$level]['file'] );
 847+ $i = 0;
 848+ $callerLine = '';
 849+ do {
 850+ $i++;
 851+ $callerLine = $lines[$backtrace[$level]['line'] - $i] . $callerLine;
 852+ } while ( stripos( $callerLine, $backtrace[$level]['function'] ) === false );
 853+ preg_match( '/([a-zA-Z0-9\_]+)::' . $backtrace[$level]['function'] . '/', $callerLine, $matches );
 854+ if ( !isset( $matches[1] ) ) {
 855+ // must be an edge case.
 856+ throw new Exception( "Could not find caller class: originating method call is obscured." );
 857+ }
 858+ switch ( $matches[1] ) {
 859+ case 'self':
 860+ case 'parent':
 861+ return get_called_class( $backtrace, $level + 1 );
 862+ default:
 863+ return $matches[1];
 864+ }
 865+ // won't get here.
 866+ case '->': switch ( $backtrace[$level]['function'] ) {
 867+ case '__get':
 868+ // edge case -> get class of calling object
 869+ if ( !is_object( $backtrace[$level]['object'] ) )
 870+ throw new Exception( "Edge case fail. __get called on non object." );
 871+ return get_class( $backtrace[$level]['object'] );
 872+ default: return $backtrace[$level]['class'];
 873+ }
 874+
 875+ default: throw new Exception( "Unknown backtrace method type" );
 876+ }
 877+ }
 878+
 879+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r109841fix bug 33832 ([SF] undefined function get_called_class() SF_FormInput.php on...foxtrott18:54, 23 January 2012