Helper classes, polymorphism and string concatenated function calling.

Helper classes, polymorphism and string concatenated function calling.

by Gareth J Barnard -
Number of replies: 0
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi,

Pau Ferrer Ocaña and I am working on MDL-35434 and in discussion with Petr Skodak I want to have a polymorhic helper class like the '/course/format/renderer.php' - '/course/format/topics/renderer.php' setup (Dan Poltawski), but at the bottom of 'filelib.php' I notice:

// try to serve general plugin file in arbitrary context
$dir = get_component_directory($component);
if (!file_exists("$dir/lib.php")) {
send_file_not_found();
}
include_once("$dir/lib.php");

$filefunction = $component.'_pluginfile';
if (function_exists($filefunction)) {
// if the function exists, it must send the file and terminate. Whatever it returns leads to "not found"
$filefunction($course, $cm, $context, $filearea, $args, $forcedownload, array('preview' => $preview));
}

send_file_not_found();

Which is performing a string concatination in order to perform a function (not method call).  But this is just a function call and not via a base class with a sibing implementing class method, so not completely OO in terms of what might be described as OO implementation, although you could argue that the implementation was still OO against OOD.  So...

1. Are there plans / policies to have helpers as classes throughout Moodle and remove concatenated string function name detection and calling?

2. What is the PHP way of finding the instantiated (or instantiating) helper object of a plugin etc.  (on the premise that themes are plugins / add-ons)?  Once I have this then I can easily see that a cast to the abstract base class and then calling a protected method would (or should) invoke the sibing classes implementation of the method as long as the reference to the helper object was indeed an instance of the sibling class.

3. Do you consider:

$filefunction = $component.'_pluginfile';
if (function_exists($filefunction)) {

to be OO? - Not a critism at all, just asking smile  And wanting thoughts on this as we are on a forum.

Cheers,

Gareth

 

Average of ratings: -