Jump to content

Recommended Posts

Would it be possible to do something like this?  I hope it's self explanatory enough. 

 

<?php
class Foo{
    public function bar($var1, $var2){
        return $var1 + $var2;
    }
}

$class='Foo';
$action='bar';
$args=array('var1'=>'5', 'var2'=>'3');

$total=$class->$action($args);

Link to comment
https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/
Share on other sites

I was in a hurry to finish that post earlier, here's an actual example I'm looking at:

 

In my registry class I have a method called useObject.  Basically that just allows me to use objects stored in the registry (which only have private access) without having to pull it out of the registry. 

$registry->useObject('template', 'load', '404');

 

In that bit, template is an object stored in the registry.  'load' is a method, and '404' is what I'm passing as an argument.  That all works fine.

 

But what if the load method needed 3 args?  My registry's useObject method needs to be able to call different methods that take various amounts of args, so I can't just send an array of args unless each method was written to accept them. 

 

I basically need to somehow do this type of 'translation':

$registry->useObject('template', 'load', 'var1', 'var2', 'var3');

OR

$registry->useObject('template', 'load', array('var1', 'var2', 'var3'));

Which should be equivalent to

$template->load('var1', 'var2', 'var3');

 

Does that make any more sense?  If it isn't possible, what would you suggest as far as a different approach to my useObject method?

 

Just popped in my head:  Is it 'better' if all functions/methods just took an array of args?  I guess that could technically work...like instead of:

function($var1, $var2){
    return $var1+$var2;
}

something like

function($args){
    return $args['var1']+$args['var2'];
}

Still not sure I understand your question but this may help.

 

A function can except an ininant amount of arguments if you don't bother declaring them in your definition. eg;

 

<?php

    function foo() {
        foreach (func_get_args() as $arg) {
            echo $arg . "\n";
        }
    }

    foo(1,2,3,4,5,6,7,8,9,'bob',4,5,6,'blah','blah','boo!');

Check out call_user_func_array().  Quick example:

 

<?php
function do_call($object, $method, $args) {
    call_user_func_array(array($object, $method), $args);
}

class Foo {
    public function test($var, $var2) {
        echo $var . $var2;
    }
}

$foo = new Foo;
do_call($foo, 'test', array('Hello,', ' World'));

 

Except you'd use an already existing object.

Essentially.  But I'm probably going to have to go out on a limb and say that the function sucked (implementation-wise) back in PHP4 because of the object model.  And since call_user_func_array() supports method calls, it's probably the new, 'correct' replacement.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.