views:

1029

answers:

3

Let's say we have a method signature like

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

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');

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);

and with 4 parameters:

$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);

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?

+5  A: 

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

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.

Tomalak
+1  A: 

Just to confirm what Tomalak stated here:

The following works:

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

Not nice - but the explanation is clear and comprehensible.

Stefan Gehrig
A: 

@ Tomalak

Actually, the default value creates a variable without any reference involved. Which is something you simply cannot kick off when you pass something.

What i find to illustrate the reasons is the following example (which i did not test):

function foo (&$ref = NULL) {
  $args = func_get_args();
  echo var_export($ref, TRUE).' - '.var_export($args, TRUE);
}
$bar = NULL;
foo();     // NULL - array()
foo($bar); // NULL - array(0 => NULL)

In my opinion, PHP should offer a way to NOT pass certain parameters, like with
foo($p1, , , $p4); or similar syntax instead of passing NULL.
But it doesn't, so you have to use dummy variables.

josh