Home  >  Article  >  Backend Development  >  php函数中传引用参数可以设置默认值吗

php函数中传引用参数可以设置默认值吗

WBOY
WBOYOriginal
2016-06-06 20:52:401874browse

像这样:

function foo(&$argument = 'bar') { ... }

这样就可以这样调用:
foo();
foo($xxx);

回复内容:

像这样:

function foo(&$argument = 'bar') { ... }

这样就可以这样调用:
foo();
foo($xxx);

可以是可以…… 不过这样有啥好处啊…… 很奇怪的用法

这个就像Java里的方法重载,是处理不固定参数个数,给参数赋默认值的方法。把Java重载需要定义多个函数,简化为只需要一个函数。

但是对于多个函数,需要注意,如果某个函数赋默认值,那么函数表里排在他后边的所有函数都要有默认值,否则参数表里的默认值没有意义,也就是说,调用的时候,只能选择 不传某个参数以及参数表其之后的所有参数。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn