Include only a script's functions

Hi,

A coworker and I were talking today about a "magic number" problem in the widgets.  If the user has not specified a number of items to display, Elgg defaults to a number.  But over on the edit side, if the user hasn't picked a number, Elgg shows the first number in the select (rather than the default).  My coworker will be submitting a trac ticket and workaround code.  But we got to talking about where would be a better place to store the default number (for our submission we will just hardcode it and add comments in both the view and edit files).

I wondered if one file could have a function or a define which could be included in the other...while NOT outputting the included file's output.  It worked!  Here's a pair of sample files:

//edit.php (the one needing the other file's functions)
<?php
echo 'This is the beginning of edit.php<br>';
require_only_functions("view.php");
echo 'The default number is: ' . getDefault() . '<br>';
echo 'Here is the define number: ' . DEFAULT_NUM;

function require_only_functions($file){
    ob_start();
    require_once $file;
    ob_end_clean();
}
?>

//here is view.php
<?php
//this file does some worthless outputting, but contains a function getDefault() and define() that edit.php needs
echo "some worthless stuff";
define("DEFAULT_NUM", 10);

function getDefault() {
    return 4;
}
?>

When edit.php is called, all that is displayed is:

This is the beginning of edit.php
The default number is: 4
Here is the define number: 10

Note that the output from view.php ("some worthless stuff") is discarded.

This is maybe a bit too tricky for general purpose use.  But I'm thinking that it might be a clever way to perform unit testing on functions which are not encased in classes.  For example, if the get_default() function was not trivial, one could use this sort of "include" to gain access to the function without gumming up your output with the rest of its script.

Mike

  • Mike, what I do is this:

    In the edit view, check if the user has set the parameter and if not, set the parameter to the default. Do nothing in view.php since the parameter has already been set in edit.php. The only thing I have not checked is how this works with the defaultwidgets plugin.

  • @Cash, the problem we're addressing is what gets displayed by view in the case where the user has not yet chosen a preferred value using edit.  What we're seeing is that the widget is displaying the default number of entities (which is good), but when the user then clicks on the Edit button, the select control is typically showing "1" (which I think is bad). 

    So for example, I just dropped the Groups Membership widget on my Profile.  It shows 4 groups.  But then I click on Edit, and it says "Number of groups to display: 1".  What I would prefer is if instead of "1" being selected, that "4" be selected since that is what the user is actually seeing.  Here's the thought bubble that I worry will happen in a user's head: "Hmmm, it says 1, but there are 4 displayed, so this must not be the place where I set the number to be displayed".

    MIke