我有一套PHP功能,需要3个参数。 我想把它当作第1和第3个参数的价值,但我希望第2个参数会落空。
我怎么能具体说明我通过什么,否则,它就被解释为我通过第1和第2个位置的数值。
感谢。
我有一套PHP功能,需要3个参数。 我想把它当作第1和第3个参数的价值,但我希望第2个参数会落空。
我怎么能具体说明我通过什么,否则,它就被解释为我通过第1和第2个位置的数值。
感谢。
不能“通过”参数,该参数在参数清单末尾:
在你的情况下,你必须给2个参数——最好是缺省——带来价值;如果是,这要求你知道这一缺省价值。
A possible alternative would be not have your function take 3 parameters, but only one, an array :
function my_function(array $params = array()) {
// if set, use $params[ first ]
// if set, use $params[ third ]
// ...
}
并称之为这一职能:
my_function(array(
first => plop ,
third => glop
));
这将使你能够:
但:
一旦确定一个缺省参数,就不需要该参数之后的所有参数。 你可以做这样的事情:
const MY_FUNCTION_DEFAULT = "default";
public function myFunction($one, $two = "default", $three = 3)
{
if (is_null($two)) $two = self::MY_FUNCTION_DEFAULT;
//...
}
// call
$this->myFunction(1, null, 3);
http://www.php.net/manual/en/Function.func-get-args.php
你可以使用反思功能。 这一问题已经由一位匿名的投稿人解决,见网站:。
For those wishing to implement call-by-name functionality in PHP, such as implemented e.g. in DB apis, here s a quick-n-dirty version for PHP 5 and up
function call_user_func_named($function, $params)
{
// make sure we do not throw exception if function not found: raise error instead...
// (oh boy, we do like php 4 better than 5, don t we...)
if (!function_exists($function))
{
trigger_error( call to unexisting function .$function, E_USER_ERROR);
return NULL;
}
$reflect = new ReflectionFunction($function);
$real_params = array();
foreach ($reflect->getParameters() as $i => $param)
{
$pname = $param->getName();
if ($param->isPassedByReference())
{
/// @todo shall we raise some warning?
}
if (array_key_exists($pname, $params))
{
$real_params[] = $params[$pname];
}
else if ($param->isDefaultValueAvailable()) {
$real_params[] = $param->getDefaultValue();
}
else
{
// missing required parameter: mark an error and exit
//return new Exception( call to .$function. missing parameter nr. .$i+1);
trigger_error(sprintf( call to %s missing parameter nr. %d , $function, $i+1), E_USER_ERROR);
return NULL;
}
}
return call_user_func_array($function, $real_params);
}
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...