PHP按引用参数,默认为null [英] PHP by-reference parameters and default null

查看:566
本文介绍了PHP按引用参数,默认为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有

public static function explodeDn($dn, array &$keys = null, array &$vals = null,
    $caseFold = self::ATTR_CASEFOLD_NONE)

我们可以通过省略$dn之后的所有参数来轻松调用该方法:

we can easily call the method by omitting all parameters after $dn:

$dn=Zend_Ldap_Dn::explodeDn('CN=Alice Baker,CN=Users,DC=example,DC=com');

我们还可以使用3个参数调用该方法:

We can also call the method with 3 parameters:

$dn=Zend_Ldap_Dn::explodeDn('CN=Alice Baker,CN=Users,DC=example,DC=com', $k, $v);

并带有4个参数:

$dn=Zend_Ldap_Dn::explodeDn('CN=Alice Baker,CN=Users,DC=example,DC=com', $k, $v, 
    Zend_Ldap_Dn::ATTR_CASEFOLD_UPPER);

但是为什么不能用以下参数组合来调用该方法:

But why is it impossible to call the method with the following parameter combination for example:

$dn=Zend_Ldap_Dn::explodeDn('CN=Alice Baker,CN=Users,DC=example,DC=com', $k, null, 
    Zend_Ldap_Dn::ATTR_CASEFOLD_UPPER);
$dn=Zend_Ldap_Dn::explodeDn('CN=Alice Baker,CN=Users,DC=example,DC=com', null, $v);

null传递给方法与依赖默认值之间有什么区别?该限制条件是否写在手册中?可以规避吗?

What's the difference between passing null to the method and relying on the default value? Is this constraint written in the manual? Can it be circumvented?

推荐答案

这是因为您无法引用null.

It's because you can't have a reference to null.

您可以引用一个包含null的变量-这正是默认值的作用.或者,您可以将null作为文字值传递-但由于您需要out参数,因此在此是不可能的.

You can have a reference to a variable that contains null - that is exactly what the default value does. Or you can pass in null as a literal value - but since you want an out parameter this is not possible here.

这篇关于PHP按引用参数,默认为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆