简体   繁体   中英

How to use a function with several arguments without defining them in advance, with PHP?

i'm searching for a solution in PHP to use a function which can use several arguments without defining in advance the number of arguments. I know there is the function func_get_args(), but it's not quite enough... In my case i have a function that render a template with twig, and I want to give context to the template, I've made that but it's not realy proper beacause if the order of arguments change when I call the function everything is broken...

Someone have a solution?... Thanks for your time!

    public function render_with_context()
    {
        $args = func_get_args();
        
        $context['loged'] = $args[0];
        $context['agencies'] = $args[1];
        $context['login'] = $args[2];
        $context['password'] = $args[3];
        $context['active_agencies'] = $args[4];
        
        \Timber\Timber::render('views/admin.html.twig', $context);
    }

I've made that, it worked realy well!

$this->render_with_context([
    'loged' => $loged, 
    'agencies' => $agencies, 
    'login' => $bdd_login, 
    'password' => $bdd_password,
    'active_agencies' => $active_agencies
]);
public function render_with_context() {
    $args = func_get_args();
    $context['loged'] = $args[0]['loged'];
    $context['agencies'] = $args[0]['agencies'];
    $context['login'] = $args[0]['login'];
    $context['password'] = $args[0]['password'];
    $context['active_agencies'] = $args[0]['active_agencies'];
    \Timber\Timber::render('views/admin.html.twig', $context);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM