follow up to r111468 - put in compatibility for php 5.2 using debug_backtrace hack
authorJeroen De Dauw <jeroendedauw@users.mediawiki.org>
Wed, 15 Feb 2012 19:39:42 +0000 (19:39 +0000)
committerJeroen De Dauw <jeroendedauw@users.mediawiki.org>
Wed, 15 Feb 2012 19:39:42 +0000 (19:39 +0000)
includes/DBTable.php

index ea627eb..ff29ab4 100644 (file)
@@ -540,12 +540,43 @@ abstract class DBTable {
                static $instance;
                
                if ( !isset( $instance ) ) {
-                       $instance = new static;
+                       $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class();
+                       $instance = new $class;
                }
                
                return $instance;
        }
 
+       /**
+        * Compatibility fallback function so the singleton method works on PHP < 5.3.
+        * Code borrowed from http://www.php.net/manual/en/function.get-called-class.php#107445
+        *
+        * @since 1.20
+        *
+        * @return string
+        */
+       protected static function get_called_class() {
+               $bt = debug_backtrace();
+               $l = count($bt) - 1;
+               $matches = array();
+               while(empty($matches) && $l > -1){
+                       $lines = file($bt[$l]['file']);
+                       $callerLine = $lines[$bt[$l]['line']-1];
+                       preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l--]['function'].'/',
+                               $callerLine,
+                               $matches);
+               }
+               if (!isset($matches[1])) $matches[1]=NULL; //for notices
+               if ($matches[1] == 'self') {
+                       $line = $bt[$l]['line']-1;
+                       while ($line > 0 && strpos($lines[$line], 'class') === false) {
+                               $line--;
+                       }
+                       preg_match('/class[\s]+(.+?)[\s]+/si', $lines[$line], $matches);
+               }
+               return $matches[1];
+       }
+
        /**
         * Get an array with fields from a database result,
         * that can be fed directly to the constructor or