0

I need to build a method column which can called with different arguments type, such like the situation here .

         ->column('herald','style')
         ->column('herald','style','styleText')
         ->column('herald',['kind','style'])
         ->column('herald',['kind','style'],['kindText','styleText'])
         ->column('item',['kind','style'],false)
         ->column('item',['kind','style'],['kindText','styleText'],false)
         ->column('herald_style','style',false)
         ->column('herald_style','style','styleText',false)

I just want the function can be called clearly ,to overwrite like the Java do, and I have been tried using the func_get_args() to handle the arguments one by one, but it`s seem worse..
Is that have any way to do ?

7
  • you may asign a default value in function parameter like function coloumn($a=null,&b=null,$c=null,$d=null){} Commented Oct 6, 2016 at 17:44
  • What you meant by the parameter - ['kind','style']? Commented Oct 6, 2016 at 17:50
  • 1
    i think he wants that the parameter may be an array.@SanjayKumarNS Commented Oct 6, 2016 at 17:51
  • @Pradyut Manna Yes I also thinks the same. Commented Oct 6, 2016 at 17:56
  • @Fan, can you explain in more details what are you trying to do? This approach seems to be not the best one. What column() does? Commented Oct 6, 2016 at 18:28

3 Answers 3

1

Accepting a variable number of arguments in a function?

If using PHP 5.6+ you can use the splat operator ... to put any submitted arguments into an array.

function column(...$args) {

   // this is how many arguments were submitted
   $number_of_arguments = count($args);

   // iterate through them if you want...
   foreach($args as $arg) {

      // ...and process the arguments one by one
      do_something_with($arg);
   }
}

See example 13 on http://php.net/manual/en/functions.arguments.php

If using an earlier version of PHP then Sanjay Kumar N S's answer will do it.

Sign up to request clarification or add additional context in comments.

Comments

0

If you mean ['kind','style'] as an array, then try this:

<?php
function column()
{
    $numargs = func_num_args();
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . (!is_array($arg_list[$i]) ? $arg_list[$i]: ' an array ' ). "<br />";
    }
    echo "<br /><br />";
}

column('herald','style');
column('herald','style');
column('herald','style','styleText');
column('herald',array('kind','style'));
column('herald',array('kind','style'),array('kindText','styleText'));
column('item',array('kind','style'),false);
column('item',array('kind','style'),array('kindText','styleText'),false);
column('herald_style','style',false);
column('herald_style','style','styleText',false);

?>

Comments

0

You can use func_get_args() at body of your function like this:

function callMe() {
    dd(func_get_args());
} 

Also if it depends on count of arguments you can use func_num_args()

http://php.net/manual/ru/function.func-get-args.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.