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 ::
}
// 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;
$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;
}