I’m creating a small LDAP connection class but this applies to PHP constants in general.
PHP has constants like LDAP_OPT_PROTOCOL_VERSION, LDAP_OPT_HOST_NAM, and LDAP_OPT_TIMELIMIT that are used in the following function:
ldap_set_option ( $myLdapConnection, LDAP_OPT_PROTOCOL_VERSION, 3 )
In my LDAP connection class I want to be able to pass the options in an array like so
array(
'LDAP_OPT_PROTOCOL_VERSION' => 3,
'LDAP_OPT_TIMELIMIT' => 1000
);
I’d then like to do the following type of loop
foreach( $options as $option => $value ){
ldap_set_option ( $myLdapConnection, $option, $value );
}
If I try this, however, I get an error that says the function expects a long, not a string. How can I get past this?
EDIT: Bart is right, I realize. I’ll do it this way. It’ll even be faster because there won’t be a string involved.
Just to be clear though, if ever there should be overlap of constants, for example when I define my own constants, then it might be a problem.
Why not build your array like this:
Also keeps it working if for whatever reason PHP changes (new PHP release for example) the values of any of the constants you use.