在PHP手册中,为了显示带有可选参数的函数的语法,他们在每组相关的可选参数周围使用了括号。例如,对于date()函数,手册如下:

string date ( string $format [, int $timestamp = time() ] )

其中$timestamp是可选参数,当为空时,它默认为time()函数的返回值。

在PHP中定义自定义函数时,如何创建这样的可选参数?


当前回答

从7.1开始,为可空参数提供了类型提示

function func(?Object $object) {}

它将在以下情况下工作:

func(null); //as nullable parameter
func(new Object());  // as parameter of declared  type

但是对于可选值签名应该是这样的。

function func(Object $object = null) {} // In case of objects
function func(?Object $object = null) {} // or the same with nullable parameter

function func(string $object = '') {} // In case of scalar type - string, with string value as default value
function func(string $object = null) {} // In case of scalar type - string, with null as default value
function func(?string $object = '') {} // or the same with nullable parameter

function func(int $object = 0) {} // In case of scalar type - integer, with integer value as default value
function func(int $object = null) {} // In case of scalar type - integer, with null as default value
function func(?int $object = 0) {} // or the same with nullable parameter

它可以被调用为

func(); // as optional parameter
func(null); // as nullable parameter
func(new Object()); // as parameter of declared type

其他回答

从7.1开始,为可空参数提供了类型提示

function func(?Object $object) {}

它将在以下情况下工作:

func(null); //as nullable parameter
func(new Object());  // as parameter of declared  type

但是对于可选值签名应该是这样的。

function func(Object $object = null) {} // In case of objects
function func(?Object $object = null) {} // or the same with nullable parameter

function func(string $object = '') {} // In case of scalar type - string, with string value as default value
function func(string $object = null) {} // In case of scalar type - string, with null as default value
function func(?string $object = '') {} // or the same with nullable parameter

function func(int $object = 0) {} // In case of scalar type - integer, with integer value as default value
function func(int $object = null) {} // In case of scalar type - integer, with null as default value
function func(?int $object = 0) {} // or the same with nullable parameter

它可以被调用为

func(); // as optional parameter
func(null); // as nullable parameter
func(new Object()); // as parameter of declared type

很像手册,在你的参数定义中使用等号(=):

function dosomething($var1, $var2, $var3 = 'somevalue'){
    // Rest of function here...
}

给可选参数一个默认值。

function date ($format, $timestamp='') {
}

下面是一些我觉得很有用的笔记:

保持默认值在右侧。 函数($var1, $var2, $var3="常量",$var4="另一个") 参数的默认值必须是常量表达式。它不能是变量或函数调用。

如果您不知道需要处理多少属性,可以使用PHP 5.6中引入的可变参数列表令牌(…)(请参阅此处的完整文档)。

语法:

function <functionName> ([<type> ]...<$paramName>) {}

例如:

function someVariadricFunc(...$arguments) {
  foreach ($arguments as $arg) {
    // do some stuff with $arg...
  }
}

someVariadricFunc();           // an empty array going to be passed
someVariadricFunc('apple');    // provides a one-element array
someVariadricFunc('apple', 'pear', 'orange', 'banana');

如您所见,这个令牌基本上将所有参数转换为一个数组,您可以以任何喜欢的方式处理该数组。