Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for bug #1555:

Now the test for block constructors works under PHP 5.
  • Loading branch information...
commit 33bee34c0b0fa389dbccaeb31c2bb87d7350cd66 1 parent dbda4a9
defacer authored
Showing with 44 additions and 3 deletions.
  1. +44 −3 lib/blocklib.php
View
47 lib/blocklib.php
@@ -14,6 +14,47 @@
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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.