致命错误:未捕获错误:函数名称必须是字符串

时间:2018-04-02 15:00:31

标签: php

最近我升级到了PHP7,以前的代码在我的旧版本的PHP上工作,我不太确定如何解决我当前的问题,是否有任何推荐阅读有人可能建议让我快速使用最新版本的PHP?我一直在慢慢完成手册,虽然一些额外的资源将被赞赏,我当前的错误在第32行,我认为它与Backward incompatible changes

有关

PHP代码

<?php 

require ("classes/Login.php");
require ("classes/Database.php");

    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $login = new Login();
        $email = $password = "";
        $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);     

        $email = $post['email'];
        $password =  $post['password'];     
        $errors = array();

        $fields = array(
        'email' => array(        
            'validate' => 'validEmail',
            'message' => 'Enter a valid email address',
            'value' => $email,
        ),
        'password' => array(         
            'validate' => 'emptyPassword',
            'message' => 'Password required',
            'value' => $password,
        )
        );


        foreach($fields as $key => $value) 
        {
            $validation_result = $login->$value['validate']($value['value']);

            if(!$validation_result) 
            {
                $errors[] = ['name' => $key, 'error' => $value['message']];
            }
        }

     if(empty($errors))  
        {  
            $db = new Database;
            $query = "SELECT userId,email,username,password FROM users WHERE email = :email";   
            $stmt = $db->prepare($query);    
            $stmt->bindValue(':email', $email);  
            $stmt->execute();   
            if(!$results = $stmt->fetch())  
            {  
                //  email did not match  
                $errors[] = ["name" => "email", "error" => "Incorrect password"];  
            }  
            else   
            {  
                // verify the password  
                if(!password_verify($password, $results['password']))  
                {  
                    // the password did not verify  
                       $errors[] = ["name" => "password", "error" => "Incorrect password"];    
                }  
                else  
                {  
                    // the password did verify 
                    session_start(); 
                    // this is the success response  
                    $success = ['response' => 'true'];   
                    $_SESSION['userId'] = $results['userId'];   
                }  
            }
        }


    }

header('Content-Type: application/json');
if (empty($errors))
{
    echo json_encode($success);
}
else
{
    echo json_encode(["errors" => $errors]);
}   

登录

class Login
{   
    private 
    $email,
    $password;


    public function validEmail($email)
    {   
        return (filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE);  

        return $email;
    }   

    public function emptyPassword($password)
    {
        return (empty($password) !== TRUE);

        return $email;
    }       
}

第32行:

$validation_result = ($login->$value)['validate']($value['value']); 

1 个答案:

答案 0 :(得分:1)

我发现......

$validation_result = {$login->$value['validate']}($value['value']);

适合我。 another similar question