]> git.mjollnir.org Git - moodle.git/commitdiff
Fix for bug #1555:
authordefacer <defacer>
Fri, 18 Jun 2004 12:25:27 +0000 (12:25 +0000)
committerdefacer <defacer>
Fri, 18 Jun 2004 12:25:27 +0000 (12:25 +0000)
Now the test for block constructors works under PHP 5.

lib/blocklib.php

index 1e412fea0da4024f43ac38438aae805fb011f74c..a003ba787cbb2a13cb394d93bab90c6774fc8282 100644 (file)
@@ -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;
         }