We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I’m used to C++ where anything can be passed to anyone. I’m wondering about the possibility of passing functions as arguments in PHP. Google has been pretty useless since any combination of those keywords gets you thousands of articles on standard function arguments.

    The code below works, more-or-less, as the C++ version would but the code is all in one file.

    <?php
    
    function foo($wd="No arg sent<br>") { 
        echo $wd;
    }
    
    
    function test($fn) {
    
       $fn("Inside Test<br>");
    
    }
    
    $bar = 'foo';
    $bar();
    $bar("Argument sent<br>");
    test($bar);
    
    ?>
    

    Output:
    No arg sent
    Argument sent
    Inside Test
    


    Here’s an example of what I’m thinking of: Say you wanted to instantiate a PHP class and set it’s sort function and compare function to use the code defined in the current file (or an included file). Something like this:

    include "tableClass.inc.php"
    
    $table = new table(10,20);
    
    mySortFunction($data, $compareFn) {
    .. etc.
    }
    myCompareFunction($s1, $s2) {
    return $s1 > $s2;
    }
    
    $sFn = 'MySortFunction';
    $cFn = 'myCompareFunction';
    
    $table->setSortFunction($sFn);
    $table->setCompareFunction($cFn);
    
    $table->sort();


    Where the code in the class would look like this:

    function sort() {
        return ($this->sortFunction($this->data,$this->compareFunction));
    }

    I can think of several uses for something like this. Does anyone know offhand whether it will work reliably in PHP or not?
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 25037
      • 32 Posts
      Well yes, that is possible as long as you guarantee that the functions are included and the names set in the table class before the sort method is called.

      But I’m not in love with that example. Objects should be modular with as few dependencies as possible. Looking at the table class, you wouldn’t know what functions may be called or where those functions reside. That’s not very transparent, and difficult to maintain. Presumably there’d be some logic to determine how the table data should be sorted. If possible, I’d put that logic in the table class. Let the table class decide how to sort itself - what sort method to call based on its state.
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: rickj at Aug 07, 2008, 09:23 PM

        If possible, I’d put that logic in the table class. Let the table class decide how to sort itself - what sort method to call based on its state.
        Or put it in a class representing the criteria for a query to any number of tables, passed around easily to table-generic and table-specific queries and functions. wink
        (end of shameful plug for use of xPDO)
          • 3749
          • 24,544 Posts
          Quote from: rickj at Aug 07, 2008, 09:23 PM

          Well yes, that is possible as long as you guarantee that the functions are included and the names set in the table class before the sort method is called.

          But I’m not in love with that example. Objects should be modular with as few dependencies as possible. Looking at the table class, you wouldn’t know what functions may be called or where those functions reside. That’s not very transparent, and difficult to maintain. Presumably there’d be some logic to determine how the table data should be sorted. If possible, I’d put that logic in the table class. Let the table class decide how to sort itself - what sort method to call based on its state.

          I agree with you, in general. I was just trying to use a simple example to make the question clear.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting