将变量作为参数默认值的 PHP 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5859376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP function with variable as default value for a parameter
提问by The Bndr
By default a PHP function uses $_GETvariables. Sometimes this function should be called in an situation where $_GETis not set. In this case I will define the needed variables as parameter like: actionOne(234)
默认情况下,PHP 函数使用$_GET变量。有时应该在$_GET未设置的情况下调用此函数。在这种情况下,我将所需的变量定义为参数,如:actionOne(234)
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 actionOneis called "directly" from an URL using the framework Yii. By handling the $_GETvariables 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.
在actionOne从一个URL使用该框架被称为“直接”的Yii。通过处理$_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).
回答by John Parker
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'] 作为参数值。(即:在函数外部处理。)
回答by Galen
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 );
回答by markus
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
}
回答by Naftali aka Neal
Yes it is impossible.
是的,这是不可能的。
The default has to be a static variable:
默认值必须是静态变量:
function actionOne( $id='something') {
//code
}
回答by Mihail Gershkovich
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"])
错误的: function 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!
我希望这有帮助!
回答by pablo henrique
You could use constant variable
你可以使用常量变量
define('ID',$_GET["ID"]);
function($id = _ID_){
//code
}

