ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Challenge 2 : moveable function arguments

July 10, 2008 6:18am

Subscribe [0]
  • #16 / Jul 10, 2008 11:17am

    codemole

    2 posts

    i’ve found a way to create a function that makes it possible to add this to all functions that could use this functionality.

    Ahah! So you know of a solution! At first i thought this was an open-ended challenge. You got me interested 😊

    @php_penguin thanks for the implementation 😉


    And i just thought of the nastiest thing yet:

    function test() {
        $args = func_get_args();
        var_dump($args);
    }
    
    class param {
        function __call ($m, $a) {
            return array ($m => $a[0]);
        }
    }
    
    $p = new param();
    
    test($p->key('val'), $p->key2('val2'));

    With php 5.3 we could even skip “$p = new param();” to make it a little shorter. I know it’s ugly as hell, but it was fun thinking this up 😛

  • #17 / Jul 10, 2008 11:22am

    wiredesignz

    2882 posts

    Try this then xwero, if you want short.

    address("street = 'fifth avenue'")
    
    function address($string)
    {
        eval('$'.$string);
    }

    EDIT:
    I don’t think I read the your other comments properly either xwero. :lol:

  • #18 / Jul 10, 2008 11:44am

    xwero

    4145 posts

    Your solution has two problems
    - it only has one argument
    - it wouldn’t work with multiple parameters

    No problem i have the privilege to trash you in MY topic :coolsmirk:

  • #19 / Jul 10, 2008 12:44pm

    wiredesignz

    2882 posts

    I could make it work man, just add semicolon seperator between value pairs and explode the $string in the function, then iterate each element and eval as you go.

    You can’t trash me xwero, You just finished telling us in another thread how patient you are. :lol:

  • #20 / Jul 10, 2008 12:44pm

    Pascal Kriete

    2589 posts

    Well, I made it work - but I sure as heck wouldn’t use it.

    class Test {
    
        public function user_info($name = 'Dude', $location = 'Ireland')
        {
            $params = get_params();
            print_r($params);        
        }
    }
    
    
    function get_params() {
        // Who called?
        $backtrace = debug_backtrace();
        $func = $backtrace[1]['function'];
        $class = $backtrace[1]['class'];
        $args = $backtrace[1]['args'];
        
        // Let's have a look at what it normally takes
        $method = new ReflectionMethod($class, $func);
        $params = $method->getParameters();
    
        // Were all the parameters passed in?
        if(count($args) == count($params))
        {
            return $args;
        }
    
        // Make an array of the defaults
        $default = array();
        foreach($params as $p)
        {
            $default[$p->getName()] = $p->getDefaultValue();
        }
        
        // And an array of the passed in arguments
        $arguments = array();
        foreach($args as $a)
        {
            $parts = explode(':', $a);
            $arguments[$parts[0]] = $parts[1];
        }
        
        // Merge and return
        return array_merge($default, $arguments);
    }
    
    $user = new Test();
    $user->user_info('location:Someplace');
    $user->user_info('name:Tom');
    $user->user_info('Tom', 'Someplace');
  • #21 / Jul 10, 2008 12:48pm

    wiredesignz

    2882 posts

    I want to trash someone too.

    Inparo, what the hell is that! :lol:

  • #22 / Jul 10, 2008 12:50pm

    Pascal Kriete

    2589 posts

    A solution to a problem that noone other than xwero will ever have 😉 .

  • #23 / Jul 10, 2008 12:52pm

    wiredesignz

    2882 posts

    Hahaha, so true, so true. Good work Inparo, at least it’ll keep xwero busy for half an hour working through your code, he just rubbishes mine instantly. :lol:

  • #24 / Jul 10, 2008 7:51pm

    Randy Casburn

    997 posts

    I love a challenge…and I have a solution too. It will accept variable arguments in any of the following forms:

    doIt($name = 'sam', $street = 'fifth avenue');
    doIt($name = 'sam');
    doIt($street = 'fifth avenue');
    doIt($name, $street);
    doIt('sam');
    doIt('fifth avenue');

    and provide output thus…

    Name: name
    Value: sam 
    Name: street
    Value: fifth avenue 
    --------------------------------------------------------------------------------
    Name: name
    Value: sam 
    --------------------------------------------------------------------------------
    Name: street
    Value: fifth avenue 
    --------------------------------------------------------------------------------
    Name: name
    Value: sam 
    Name: street
    Value: fifth avenue 
    --------------------------------------------------------------------------------
    Name: name
    Value: sam 
    --------------------------------------------------------------------------------
    Name: street
    Value: fifth avenue

    In fact, it will take any arguments you want to feed it in any order as long as they are scalars.  It could easily be modified to accept arrays too, but that was outside the scope of the challenge 😜

    Now it comes down to a LOC challenge - xwero mine fits in 11 lines with single commands per line and opening braces in line and closing braces on their own lines.

    I do have to admit there is one kludgy aspect to my solution.  But hey… it works in 11 lines.

    Randy

  • #25 / Jul 10, 2008 8:38pm

    Randy Casburn

    997 posts

    If you don’t care about the name of the variable (that’s the hard part)—it fits in one (1) line of code and produces:

    Value: sam 
    Value: fifth avenue

    from

    doIt($name = 'sam', $street = 'fifth avenue');

    Randy

  • #26 / Jul 10, 2008 9:04pm

    Randy Casburn

    997 posts

    xwero—can you explain this statement:

    @mironcho : i assumed the function argument all have a default otherwise it would be an (almost?) impossible challenge.

    Even my one liner can take any number of parameters, in any order, without default values, or any predefinition of any kind. 

    The only exception to this is if you want to have a variable name available with the data.  For instance, if you pass ‘Johnny Appleseed’ to a non-determinate function such as this, there is no way for this function to know about any kind of meta data associations linked to this data. That would be perfect for simple math problems or string concatenations (like the Functional Programing thread the other day). On the other hand, if you send in $address = ‘East Blvd’ the function now can extrapolate that ‘East Blvd’ and $address have some intrinsic relationship.  So with all this in mind, it should be reasonable to expect that the relationship between a variable name and the data it contains must be established in advance.

    If you put this in the function call you make this a mandatory construct.  Remove it from the function declaration and you have the flexibility you need.  There is just one little dilemma that always creeps into the equation that makes the solution a kludge.  But it still works…and it is still in one (1) line or 11 if you want the whole shebang.

    Let me know when you’re ready for it.

    Randy

  • #27 / Jul 11, 2008 4:02am

    xwero

    4145 posts

    Randy i think the problem with parameters without a default value is that they produce an error.

    function one($one)
    {
      echo $one;
    }
    // usage
    one('word');
    one(); // error

    I think if the parameters need to checked for errors the solution will need some magic i think.

    The idea is to have movable parameters but keeping it as close as possible to the normal way php function react.

    At the moment i have 20 lines of working code but i think it can be compacted. I’m going to work on the error part just to check if it’s possible 😊

  • #28 / Jul 11, 2008 4:11am

    xwero

    4145 posts

    Inparo it looks like a very flexible solution why wouldn’t you want to use it?

    I guess i have the php4 compatible solution, without the fancy Reflectionmethod class 😉, and two arguments.

  • #29 / Jul 11, 2008 5:54am

    m4rw3r

    647 posts

    Inparo, I suggest that you also add reflection function to your code, then it would also work for functions.
    It reminds me a bit of JavaScript.

    But has anyone made any benchmarks? Like comparing some of the algorithms listed above with the passing of parameters in an array?
    It would be nice to know how much this degrades performance.

  • #30 / Jul 11, 2008 7:26am

    xwero

    4145 posts

    I’ve got an average of 7 times slower than calling a regular function, the highest iteration i did was 10000.

    BTW wiredesignz i tried your eval solution and i got a unexpexted $ in eval function error, any idea why?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases