PHP函数,变量为参数的默认值

时间:2022-09-11 17:09:05

By default a PHP function uses $_GET variables. Sometimes this function should be called in an situation where $_GET is not set. In this case I will define the needed variables as parameter like: actionOne(234)

默认情况下,PHP函数使用$_GET变量。有时,在不设置$_GET的情况下应该调用这个函数。

To get an abstract code I tried something like this:

为了得到一个抽象的代码,我尝试了以下方法:

function actionOne($id=$_GET["ID"])

which results in an error:

会导致错误:

Parse error: syntax error, unexpected T_VARIABLE

解析错误:语法错误,意外的T_VARIABLE。

Is it impossible to define an default parameter by using an variable?

是否不可能通过使用变量来定义默认参数?

Edit

编辑

The actionOne is called "directly" from an URL using the framework Yii. By handling the $_GET variables outside this function, I had to do this on an central component (even it is a simple, insignificant function) or I have to change the framework, what I don't like to do.

使用框架Yii从URL中“直接”调用actionOne。通过处理这个函数之外的$_GET变量,我必须在一个中心组件(即使它是一个简单的、不重要的函数)上做这件事,或者我必须修改框架,我不喜欢做什么。

An other way to do this could be an dummy function (something like an pre-function), which is called by the URL. This "dummy" function handles the variable-issue and calls the actionOne($id).

另一种方法是虚拟函数(类似于预函数),URL调用它。这个“哑”函数处理可变问题并调用actionOne($id)。

5 个解决方案

#1


45  

No, this isn't possible, as stated on the Function arguments manual page:

不,这是不可能的,如功能参数手册页所述:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

默认值必须是常量表达式,而不是(例如)变量、类成员或函数调用。

Instead you could either simply pass in null as the default and update this within your function...

相反,您可以简单地传入null作为默认值,并在函数中更新它……

function actionOne($id=null) {
    $id = isset($id) ? $id : $_GET['ID'];
    ....
}

...or (better still), simply provide $_GET['ID'] as the argument value when you don't have a specific ID to pass in. (i.e.: Handle this outside the function.)

…或者(更好的是),当没有特定的ID传入时,只需提供$_GET['ID']作为参数值。(即。:在功能之外处理。

#2


4  

function actionOne( $id=null ) {
    if ($id === null) $id = $_GET['ID'];
}

But, i would probably do this outside of the function:

但是,我可能会在函数之外这样做:

// This line would change, its just a for instance
$id = $id ? $id : $_GET['id'];
actionOne( $id );

#3


3  

You should get that id before you call the function. Checking for the existence of the parameter breaks encapsulation. You should do something like that:

在调用函数之前,您应该得到这个id。检查参数是否存在破坏封装。你应该这样做:

if (isset($_GET["ID"])
{
    $id = $_GET["ID"];
}
else
{
    //$id = something else
}

function doSomethingWithID($id)
{
    //do something
}

#4


1  

Yes it is impossible.

是的,这是不可能的。

The default has to be a static variable:

默认值必须是一个静态变量:

function actionOne( $id='something') {
    //code
}

#5


1  

Easy peanuts!
(Might contain minor mistakes, errors or typos!)

容易花生!(可能包含小错误、错误或拼写错误!)

You need a helper function, which will call you main function recursively, but having NULL as default:

你需要一个助手函数,它会递归地调用你的主函数,但默认值为NULL:

Wrong: function actionOne($id=$_GET["ID"])

错误:函数actionOne($ id = $ _GET[" id "])

Right:

正确的:

function actionOne($id) {...}
function actionOnewithID($id=NULL) {
  if (NULL==$id){actionOne($_GET["ID"]);}
  else {actionOne($id);
}

And if you need to return a value:

如果你需要返回一个值:

function actionOne($id) {...}
function actionOnewithID($id=NULL) {
  if (NULL==$id){return(actionOne($_GET["ID"]));}
  else {return(actionOne($id));
}

I hope this helps!

我希望这可以帮助!

#1


45  

No, this isn't possible, as stated on the Function arguments manual page:

不,这是不可能的,如功能参数手册页所述:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

默认值必须是常量表达式,而不是(例如)变量、类成员或函数调用。

Instead you could either simply pass in null as the default and update this within your function...

相反,您可以简单地传入null作为默认值,并在函数中更新它……

function actionOne($id=null) {
    $id = isset($id) ? $id : $_GET['ID'];
    ....
}

...or (better still), simply provide $_GET['ID'] as the argument value when you don't have a specific ID to pass in. (i.e.: Handle this outside the function.)

…或者(更好的是),当没有特定的ID传入时,只需提供$_GET['ID']作为参数值。(即。:在功能之外处理。

#2


4  

function actionOne( $id=null ) {
    if ($id === null) $id = $_GET['ID'];
}

But, i would probably do this outside of the function:

但是,我可能会在函数之外这样做:

// This line would change, its just a for instance
$id = $id ? $id : $_GET['id'];
actionOne( $id );

#3


3  

You should get that id before you call the function. Checking for the existence of the parameter breaks encapsulation. You should do something like that:

在调用函数之前,您应该得到这个id。检查参数是否存在破坏封装。你应该这样做:

if (isset($_GET["ID"])
{
    $id = $_GET["ID"];
}
else
{
    //$id = something else
}

function doSomethingWithID($id)
{
    //do something
}

#4


1  

Yes it is impossible.

是的,这是不可能的。

The default has to be a static variable:

默认值必须是一个静态变量:

function actionOne( $id='something') {
    //code
}

#5


1  

Easy peanuts!
(Might contain minor mistakes, errors or typos!)

容易花生!(可能包含小错误、错误或拼写错误!)

You need a helper function, which will call you main function recursively, but having NULL as default:

你需要一个助手函数,它会递归地调用你的主函数,但默认值为NULL:

Wrong: function actionOne($id=$_GET["ID"])

错误:函数actionOne($ id = $ _GET[" id "])

Right:

正确的:

function actionOne($id) {...}
function actionOnewithID($id=NULL) {
  if (NULL==$id){actionOne($_GET["ID"]);}
  else {actionOne($id);
}

And if you need to return a value:

如果你需要返回一个值:

function actionOne($id) {...}
function actionOnewithID($id=NULL) {
  if (NULL==$id){return(actionOne($_GET["ID"]));}
  else {return(actionOne($id));
}

I hope this helps!

我希望这可以帮助!