ldap_modify dos在函数中不起作用,抛出“Object class violation”错误

I created a php code that updates the user's email in AD. It works!
But I want to write code in functions. When I wrote the same code as the functions, ldap_modify throws "Object class violation" error.

Sorry about My bad ENG!

I checked all the variables, before function and in the function and everything seems ok. Or I'm blind and can't see a mistake.

This code works, and email is updated in AD

$entry["mail"] = 'foo@bar.com';
$result = ldap_modify($ldap->getLdap_connection(), $user_dn, $entry);
if ($result === false) {
   echo 'error';
}else{
   echo 'OK';
}

But when the code is written as a function, code throws "Object class violation" error.


function update_field($connection, $dn, $entry, $debug){
    $error = array();
    $result = NULL;

    if($connection === NULL){
       $error[] = 'LDAP connection is NULL'; 
    }

    if($dn === NULL){
       $error[] = 'User DN is NULL';
    }

    if($entry === NULL){
      $error[] = 'Field is NULL'; 
    }

    if($debug){ }        

    if(empty($error)){
        $result = ldap_modify($connection, $dn, $entry);        
        if($result === false) {
            $result = ldap_error($connection);
        }else{
            $result = 'OK';
        }
    }else{
        $result = $error;
    }
    return $result;
}

$entry["mail"] = 'foo@bar.com';
$update =  update_field($ldap->getLdap_connection(), $dn, $entry, 1);