PHP 在函数变量不存在时跳过它


PHP Skip Function Variables When It Doesn't Exist

我有这段代码:

public function changeProfile ($first_name, $last_name, $email, $phone, $password, 
                             $bank_name, $bank_account_number, $bank_account_name, 
                             $gender, $birthday, $address, $area, $default_type) {
    $this->connect();
    $data = array ('first_name' => $this->escapeString($first_name),
                   'last_name' => $this->escapeString($last_name),
                   'email' => $this->escapeString($email),
                   'phone' => $this->escapeString($phone),
                   'password' => $this->escapeString($password), 
                   'bank_name' => $this->escapeString($bank_name),
                   'bank_account_number' => $this->escapeString($bank_account_number),
                   'bank_account_name' => $this->escapeString($bank_account_name),
                   'gender' => $this->escapeString($gender),
                   'birthday' => $this->escapeString($birthday),
                   'address' => $this->escapeString($address),
                   'area' => $this->escapeString($area),
                   'default_type' => $this->escapeString($default_type));
    $this->update('user', $data, 'email = "'.$email.'" AND phone = "'.$phone.'"');
    $res = $this->getResult();      
}

现在,我有一个这样的问题:

我想"跳过"函数上的一些变量,例如 $birthday$gender ,这样它就不会在 SQL UPDATE 上处理并让当前数据保持原样。

我的意思是,如果$birthday$gender data不存在,那么不要更新表上的现有数据。 因为当我尝试将 NULL 用作函数上的变量时,我的数据被替换为空数据。

如何在没有多个IF检查每个变量的情况下管理这种情况?

谢谢

如果你有这个选项,你应该用一个参数数组替换函数的参数列表,例如:

public function changeProfile($variables)
{
    $this->connect();
    $data = array();
    foreach ($variables as $key => $value) {
        $data[$key] = $this->escapeString($value);
    }
    // Validation?
    if (!isset($data['email'], $data['phone'])) {
        die('Required fields missing.');
    }
    $this->update('user', $data, 'email = "' . $data['email'] . '" AND phone = "' . $data['phone'] . '"');
    $res = $this->getResult();
}

这假定 SQL 查询是静态的。如果需要,您还可以向其添加更多字段。

然后你会这样称呼它:

$test->changeProfileUpdated([
    'first_name' => 'john',
    'last_name' => 'doe',
    'email' => 'example@example.com',
    'phone' => 12345,
    'password' => 'password1',
    'bank_name' => 'ANZ',
    'bank_account_number' => '123-456',
    'bank_account_name' => 'J DOE',
    'gender' => 'M',
    'birthday' => '1/2/13',
    'address' => '12 Fake St',
    'area' => 'Area 51',
    'default_type' => 'Some default'
]);

下面是一个演示,比较了你的代码、此示例和验证失败。

如果您不能更改方法签名以采用数组,则可以根据数据需求自然地使用 isset()empty()检查每个值,例如:

if (!empty($gender) {
    $data['gender'] => $this->escapeString(escapeString);
}

然而,这很乏味。更好的方法是将数组作为输入,然后具有有效和/或必需键的数组。像这样:

class MyClass
{
    private $valid_keys = [
        "first_name", "last_name", "email", "phone", "password", 
        "bank_name", "bank_account_number", "bank_account_name", 
        "gender", "birthday", "address", "area", "default_type"
    ];
    public function changeProfile($profile)
    {
        // Return valid keys
        $profile = array_filter($profile, function ($key) {
            return in_array($key, self::$valid_keys);
        } , ARRAY_FILTER_USE_KEY);
        // Escape values
        $profile = array_map(function ($value) {
            // .. your escape function
        }, $profile);
        // Do your stuff
        $this->update('user', $profile, 'email = "'. $profile['email'].'" AND phone = "'.$profile['phone'].'"');
        $res = $this->getResult();      
    }
}

这样,您只会设置有效数据,并且您有办法通常对其进行转义。

哦,是的,当然只有$profile中的值才会发送到您的更新函数。

如果无法更改方法签名,则可以使用 func_get_args() 进行研究。

public function changeProfile($first_name, $last_name, $email, $phone, $password, 
                         $bank_name, $bank_account_number, $bank_account_name, 
                         $gender, $birthday, $address, $area, $default_type)
{
    $args = func_get_args();
    $profile = [];
    // Important: $valid_keys should be the same as your function parameters and in the same order for this to work. If you want to go further you can look at `ReflectionMethod` to get the names through reflection.
    foreach ($args as $index => $value) {
        $key = self::$valid_keys[$index];
        $profile[$key] = $this->escapeValue($value);
    }
    // Do your stuff
    $this->update('user', $profile, 'email = "'. $profile['email'].'" AND phone = "'.$profile['phone'].'"');
    $res = $this->getResult();
}

检查它是否为 null,然后将其保存在数组中,如下所示

function changeProfile ($first_name, $last_name,$email,$phone) {
   $this->connect();
 $data=array();
 if($first_name!=""){
     $data['first_name']= $this->escapeString($first_name);
 }
  if($last_name!=""){
     $data['last_name']=$this->escapeString($last_name);
 }
if($email!=""){
     $data['email']=$this->escapeString($email);
 }
if($phone!=""){
     $data['phone']=$this->escapeString($phone);
 }
    $this->update('user', $data, 'email = "'.$email.'" AND phone = "'.$phone.'"');
    $res = $this->getResult();

}