1

Possible Duplicate:
Any way to specify optional parameter values in PHP?
How would I skip optional arguments in a function call?

i have this declaration :

public function table_creator($thread, $table_body, $class='datagrid', $caption=null){
     some code
}

and when i execute it like that :

<?php echo SomeClass::table_creator($this->report_keys, $report, $caption="Answers"); ?>

in variable $class there is "Answers" insted of "datagrid", is there a way to not pass $class for it to work ??

Community
  • 1
  • 1
Viszman
  • 1,378
  • 1
  • 17
  • 47
  • The parameter passing takes the order in which in they are declared in the function. It will not know which to escape as default. – Arjun Abhynav Jan 02 '13 at 10:09

7 Answers7

2

PHP doesn't have named parameters (or keyword arguments). I recommend you try the following style:

public function table_creator($thread, $table_body, $options=array()){
  $default_options = array(
    "class" => "datagrid",
    // you can add more default options here
  );
  $options = array_merge($default_options, $options);
  // some code
}

Then you can call it like this:

echo SomeClass::table_creator($this->report_keys, $report,
                              array("caption" => "Answers");
Xiao Jia
  • 4,169
  • 2
  • 29
  • 47
1

The parameter passing takes the order in which in they are declared in the function. It will not know which to escape as default.

The default arguments will be overridden LEFT to RIGHT.

If you do well know that 'datagrid' will be the value of $class, why don't you pass it like this?

<?php 
 echo SomeClass::table_creator($this->report_keys, $report,'datagrid',$caption='Answers');
?>
Arjun Abhynav
  • 543
  • 4
  • 15
0

PHP doesn't work that way, you'd need to change the order of the $class and $caption parameters for that usage to work. By using $caption="Answers" in the function call you are assigning a variable value and passing a string "Answers" as the third argument of the function.

scottlimmer
  • 2,230
  • 1
  • 22
  • 29
0

If you pass empty value for that class parameter it will work by taking the value as datagrid as default one

You have to declare the function as STATIC to call directly using ::

So declaration should be,

public static function table_creator($thread, $table_body, $class='datagrid', $caption=null){
     some code
}

Then, why are you passing the third parameter by assigning the value,

Change the below code from

<?php echo SomeClass::table_creator($this->report_keys, $report, $caption="Answers"); ?>

TO

<?php echo SomeClass::table_creator($this->report_keys, $report, "Answers"); ?>

OR

<?php echo SomeClass::table_creator($this->report_keys, $report); ?>
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
0

The declaration has four parameters and you pass only three. If you want to override the default value of the fourth parameter, you will have to explicitly pass the third as well:

echo SomeClass::table_creator($this->report_keys, $report, 'datagrid', "Answers"); ?>

The method you use now does not work in PHP, although it does work in some other languages.

If you need to do this often you can create a function to do it for you:

function createTableDataGrid($keys, $report, $caption)
{
  echo table_creator($keys, $report, 'datagrid', $caption); ?>
}

If the class is yours to edit, you can do this in an extra method of the class instead of a separate function.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

Instead of $caption="Answers" use this code for calling member function

$caption="Answers" echo SomeClass::table_creator($this->report_keys, $report, $caption); ?>

prasobh
  • 95
  • 3
0

In PHP, function receives parameters in the same order in which there are declared. So if your function receives 4 parameters and you pass only 3 then the last parameters gets nothing.

Also just because you use $caption="Answers" does not mean the called function will receive that into its $caption paramters. Long story short, make sure that the parameters match up. If you want to skip one parameter use null so that the default value declared will be used.

So you can solve your problem with something like this:

<?php
echo SomeClass::table_creator($this->report_keys, 
                              $report, 
                              null,
                              $caption="Answers"); ?>

Just as a side reminder, you are accessing an instance method as though it is a class/static method. You might want to change that.

shawndreck
  • 2,039
  • 1
  • 24
  • 30