致命错误:未捕获ArgumentCountError:函数参数太少

时间:2017-04-20 13:49:59

标签: php oop

我知道有一些与此相关的问题,但有c ++或其他语言。我收到此错误,我不确定我的功能有什么问题。

我的错误如下:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function User::register(), 2 passed in C:\xampp\htdocs\register.php on line 39 and exactly 5 expected in C:\xampp\htdocs\classes\users.php:22 Stack trace: #0 C:\xampp\htdocs\register.php(39): User->register('ds', 'dsssssss') #1 {main} thrown in C:\xampp\htdocs\classes\users.php on line 22

我的功能是:

public function register($name, $surname, $username, $password, $email)
{
    try {
        $newPassword = password_hash($password, PASSWORD_DEFAULT);

        $stmt = $this->conn->prepare("INSERT INTO user(name, surname, username, password, email) 
                             VALUES(:name, :surname, :username, :password, :email)");

        $stmt->bindParam(":name", $name);
        $stmt->bindParam(":surname", $surname);
        $stmt->bindParam(":username", $username);   
        $stmt->bindParam(":password", $password);
        $stmt->bindParam(":password", $password);
        $stmt->bindParam(":email", $email);

        $stmt->execute();   

        return $stmt;   
}
    catch(PDOException $e) {
            echo $e->getMessage();
    }
}

Register.php文件:

  <!DOCTYPE html>
<?php
session_start();
require_once('classes/users.php');
$user = new User();

if($user->isLoggedIn()!="") {
    $user->redirect('home.php');
}

if(isset($_POST['submit'])) {
    $name = strip_tags($_POST['name']);
    $surname = strip_tags($_POST['surname']);
    $username = strip_tags($_POST['username']);
    $password = strip_tags($_POST['password']);
    $email = strip_tags($_POST['email']);

if($name=="") {
        $error[] = "provide username !";    
} else if($surname=="") {
        $error[] = "Provide surname!";  
  } else if ($username =="") {
      $error[] = "Provide username!";
    } else if($password=="") {
        $error[] = "provide password !";
      } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error[] = 'Please enter a valid email address !';
        } else if(strlen($password) < 6){
            $error[] = "Password must be atleast 6 characters"; 
          } else {
                try {
                    $stmt = $user->runQuery("SELECT username FROM user WHERE username=:username");
                    $stmt->execute(array(':username'=>$username));
                    $row=$stmt->fetch(PDO::FETCH_ASSOC);

                    if($row['username']==$username) {
                            $error[] = "sorry username already taken !";
                    } else {
                        if($user->register($username,$password)){   
                            $user->redirect('register.php?joined');
                        }
                    }
                }
                catch(PDOException $e) {
                    echo $e->getMessage();
                }
            }   
}
?>
<html>
<head>
    <meta charset="UTF-8">
    <title>
    </title>
</head>
<body>
    <div class="form">
        <form method ="post" action="register.php">
            <h3 class = "signup"> Sign Up </h3>
             <?php
        if(isset($error)) {
                        foreach($error as $error)
         {
                 ?>
                               <div class="alert alert-danger">
                    <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?>
                 </div>
                 <?php
            }
        }
        else if(isset($_GET['joined']))
        {
             ?>
             <div class="alert alert-info">
                  <i class="glyphicon glyphicon-log-in"></i> &nbsp; Successfully registered <a href='index.php'>login</a> here
             </div>
             <?php
        } ?>

            Vardas:<br>
            <input type="text" name="name" id="name" placeholder="Vardas" required>
            <br>
            Pavardė:<br>
            <input type="text" name="surname" id="surname" placeholder="Pavardė" required>
            <br>
            Prisijungimo vardas:<br>
            <input type="text" name="username" id="username" placeholder="Prisijungimo vardas" required>
            <br>
            Slaptažodis:<br>
            <input type="password" name="password" id="password" placeholder="Slaptažodis" required>
            <br>
            El. pašto adresas: <br>
            <input type="email" name="email" id="email" placeholder="El. pašto adresas" required>
            <br><br>
            <div class ="div">
            <input type="submit" name="submit" id="submit" value="Registruotis">
            <br><br>
            <label>Have an account? <a href="index.php">Sign In</a></label>
            </form> 
    </div>
</body>

感谢您的帮助!

5 个答案:

答案 0 :(得分:14)

在php 7中而不是:

    public function register($name, $surname, $username, $password, $email)
{
...

使用:

public function register($name = null, $surname = null, $username = null, $password = null, $email = null)
{
...

答案 1 :(得分:8)

我的托管公司将我们的PHP版本从7.0.x更新到7.1.x后,我遇到了同样的错误。他们认为,由于它是一个小的更新,它应该与以前的版本兼容。他们错了:here's来自迁移页面的不兼容列表。

在这个特殊情况下,之前会抛出关于函数调用缺少参数的警告的代码现在在OP的问题中抛出致命错误。

解决方案显然是提供其他答案所表明的适当参数。

答案 2 :(得分:5)

你的方法需要5个参数,你只传递2:User->register('ds', 'dsssssss')

Register.php中修改此行:

$user->register($username, $password)

$user->register($name, $surname, $username, $password, $email)

此外,您有两行$stmt->bindParam(":password", $password);

答案 3 :(得分:0)

对于遇到此错误或类似问题的任何人,下面的答案有效! 尝试使旧版本的WordPress WooCommerce在PHP 7.2上运行时遇到此错误。大声笑,我知道。 WooCommerce产品编辑屏幕为空白,出现以下错误(这破坏了产品标签)

Fatal error: Uncaught ArgumentCountError: Too few arguments to function product_custom_tab::product_tab_options(), 0 passed in wp-includes/class-wp-hook.php on line 286 and exactly 1 expected in /wp-content/plugins/woo-product-tab/includes/product_custom_tab.php:64 Stack trace: #0 wp-includes/class-wp-hook.php(286):

当转到product_custom_tab.php的第64行时。 我改变了

public function product_tab_options($product_type)

public function product_tab_options($product_type = null)

它奏效了! 感谢以下贡献者!你真的让我开心。感谢您在这里。谢谢!

Bytw:我曾尝试将其发布为评论,但不允许我这样做,因此我将其发布为答案。

答案 4 :(得分:0)

遇到同样的问题。原来托管公司已经更新了 php 版本。我刚刚在数组中添加了另一个参数,并给了它一个 null 值