Index: trunk/phase3/includes/DBTable.php |
— | — | @@ -540,13 +540,44 @@ |
541 | 541 | static $instance; |
542 | 542 | |
543 | 543 | if ( !isset( $instance ) ) { |
544 | | - $instance = new static; |
| 544 | + $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); |
| 545 | + $instance = new $class; |
545 | 546 | } |
546 | 547 | |
547 | 548 | return $instance; |
548 | 549 | } |
549 | 550 | |
550 | 551 | /** |
| 552 | + * Compatibility fallback function so the singleton method works on PHP < 5.3. |
| 553 | + * Code borrowed from http://www.php.net/manual/en/function.get-called-class.php#107445 |
| 554 | + * |
| 555 | + * @since 1.20 |
| 556 | + * |
| 557 | + * @return string |
| 558 | + */ |
| 559 | + protected static function get_called_class() { |
| 560 | + $bt = debug_backtrace(); |
| 561 | + $l = count($bt) - 1; |
| 562 | + $matches = array(); |
| 563 | + while(empty($matches) && $l > -1){ |
| 564 | + $lines = file($bt[$l]['file']); |
| 565 | + $callerLine = $lines[$bt[$l]['line']-1]; |
| 566 | + preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l--]['function'].'/', |
| 567 | + $callerLine, |
| 568 | + $matches); |
| 569 | + } |
| 570 | + if (!isset($matches[1])) $matches[1]=NULL; //for notices |
| 571 | + if ($matches[1] == 'self') { |
| 572 | + $line = $bt[$l]['line']-1; |
| 573 | + while ($line > 0 && strpos($lines[$line], 'class') === false) { |
| 574 | + $line--; |
| 575 | + } |
| 576 | + preg_match('/class[\s]+(.+?)[\s]+/si', $lines[$line], $matches); |
| 577 | + } |
| 578 | + return $matches[1]; |
| 579 | + } |
| 580 | + |
| 581 | + /** |
551 | 582 | * Get an array with fields from a database result, |
552 | 583 | * that can be fed directly to the constructor or |
553 | 584 | * to setFields. |