From: defacer Date: Fri, 18 Jun 2004 12:25:27 +0000 (+0000) Subject: Fix for bug #1555: X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=33bee34c0b0fa389dbccaeb31c2bb87d7350cd66;p=moodle.git Fix for bug #1555: Now the test for block constructors works under PHP 5. --- diff --git a/lib/blocklib.php b/lib/blocklib.php index 1e412fea0d..a003ba787c 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -14,6 +14,47 @@ define('COURSE_FORMAT_WEEKS', 0x02); define('COURSE_FORMAT_TOPICS', 0x04); define('COURSE_FORMAT_SOCIAL', 0x08); +// Returns the case-sensitive name of the class' constructor function. This includes both +// PHP5- and PHP4-style constructors. If no appropriate constructor can be found, returns NULL. +// If there is no such class, returns boolean false. +function get_class_constructor($classname) { + // Caching + static $constructors = array(); + + if(!class_exists($classname)) { + return false; + } + + // Tests indicate this doesn't hurt even in PHP5. + $classname = strtolower($classname); + + // Return cached value, if exists + if(isset($constructors[$classname])) { + return $constructors[$classname]; + } + + // Get a list of methods. After examining several different ways of + // doing the check, (is_callable, method_exists, function_exists etc) + // it seems that this is the most reliable one. + $methods = get_class_methods($classname); + + // PHP5 constructor? + if(phpversion() >= '5') { + if(in_array('__construct', $methods)) { + return $constructors[$classname] = '__construct'; + } + } + + // If we have PHP5 but no magic constructor, we have to lowercase the methods + $methods = array_map('strtolower', $methods); + + if(in_array($classname, $methods)) { + return $constructors[$classname] = $classname; + } + + return $constructors[$classname] = NULL; +} + //This function retrieves a method-defined property of a class WITHOUT instantiating an object //It seems that the only way to use the :: operator with variable class names is eval() :( //For caveats with this technique, see the PHP docs on operator :: @@ -463,8 +504,8 @@ function upgrade_blocks_plugins($continueto) { } // Let's see if it supports some basic methods - $methods = get_class_methods($classname); - if(!in_array(strtolower($classname), $methods)) { + $constructor = get_class_constructor($classname); + if(empty($constructor)) { // No constructor $notices[] = "Block $blockname: class does not have a constructor"; $invalidblocks[] = $blockname; @@ -476,7 +517,7 @@ function upgrade_blocks_plugins($continueto) { $blockobj = New $classname($site); // Inherits from MoodleBlock? - if(!is_subclass_of($blockobj, "moodleblock")) { + if(!is_subclass_of($blockobj, 'moodleblock')) { $notices[] = "Block $blockname: class does not inherit from MoodleBlock"; continue; }