Fatal error: Uncaught ArgumentCountError: Too few arguments to function
Asked Answered
G

7

15

I know there was a some questions related to this, but there are in c++ or other languages. I get this error and I'm not sure what is wrong with my function.

My error looks like this:

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

And my function is:

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 file:

  <!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>

Thank you for trying to help!

Gaol answered 20/4, 2017 at 13:49 Comment(1)
Sometimes you invoke a method twice in two different files. And you try to modify (increase the number) the arguments of the first case and the method signature as well but forget to modify the second case of the method call. When the app comes to the second case of method call, this error happens too. So check this first, before setting $arg = null in the method signature.Grissel
A
11

Your method needs 5 arguments, you only pass 2: User->register('ds', 'dsssssss')

Edit this line in Register.php:

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

to

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

Additionally you have this line twice $stmt->bindParam(":password", $password);

Amphithecium answered 20/4, 2017 at 13:52 Comment(4)
But in the registration form I have 3 other arguments, which is name, surname and username, why those values haven't passed?Gaol
I can't answer this because you didn't post that part of the code. We need this file: C:\xampp\htdocs\register.php. And probably the HTML form, too.Amphithecium
Updated my answer.Amphithecium
Thank you very much! Could you also tell me some remarks about my code?Gaol
L
33

In php 7 instead of:

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

use:

public function register($name = null, $surname = null, $username = null, $password = null, $email = null)
{
...
Logarithmic answered 25/7, 2017 at 13:29 Comment(2)
This is a great workaround, sometimes a function works fine without parameters even though you have the possibility to provide them.Idoux
Well, this is not really a 'workaround' but rather a way PHP 7+ defines what parameters are mandatory and which ones are optional.Firewood
A
11

Your method needs 5 arguments, you only pass 2: User->register('ds', 'dsssssss')

Edit this line in Register.php:

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

to

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

Additionally you have this line twice $stmt->bindParam(":password", $password);

Amphithecium answered 20/4, 2017 at 13:52 Comment(4)
But in the registration form I have 3 other arguments, which is name, surname and username, why those values haven't passed?Gaol
I can't answer this because you didn't post that part of the code. We need this file: C:\xampp\htdocs\register.php. And probably the HTML form, too.Amphithecium
Updated my answer.Amphithecium
Thank you very much! Could you also tell me some remarks about my code?Gaol
K
8

I experienced this same error after my hosting company updated our PHP version from 7.0.x to 7.1.x. They assumed that since it was a minor update, it should be compatible with previous versions. They were wrong: here's a list of the incompatibilities from the migration page.

In this particular case code that would previously throw a warning about the lack of parameters for a function call is now throwing the fatal error in the OP's question.

The solution is obviously to provide the appropriate parameters as the other answers have indicated.

Karikaria answered 23/8, 2017 at 20:10 Comment(0)
P
3

For anyone who encountered this error or something similar, the answer below works! I encountered this error when trying to get an older version of WordPress WooCommerce to run on PHP 7.2. Lol, I know. The WooCommerce Product Edit Screen was blank with the error below (which broke the product tabs)

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):

When going to line 64 of product_custom_tab.php. I changed

public function product_tab_options($product_type)

to

public function product_tab_options($product_type = null)

And it worked! Thanks to the contributors below! You really made my day. Thanks for being here. Thank you!

Bytw: I tried to post this as a comment, but it wouldn't let me, so I posted this as an answer instead.

Pyromancy answered 17/5, 2019 at 8:7 Comment(0)
S
1

Encountered the same issue. Turns out the hosting company had updated the php version. i just added another parameter to the array and gave it a value of null

Socha answered 25/3, 2021 at 7:59 Comment(1)
Could you please add the parameter you added to your answer so that others can comprehend what they have to change?Selfinsurance
I
0

In case anyone finds this issue with PHP 8 randomly throwing errors like these (even when using the functions correctly) :

PHP Fatal error: Uncaught ArgumentCountError: time() expects exactly 0 arguments, 1 given in blablabla:15

(that was calling the date function without a second argument) or

PHP Fatal error: Uncaught ArgumentCountError: is_dir() expects exactly 1 argument, 0 given in blablabla:15

(and that was using is_dir with exactly a 1 non-null argument)

In some cases seems to be caused by Zend Opcache. Might be a bug, so try disabling it :

Check (e.g. for fedora/centos with remi packages)

for zts-php

/etc/php-zts.d/10-opcache.ini

just php

/etc/php.d/10-opcache.ini (regular php)

and change

opcache.enable_cli = 0

for cli usage, and/or

opcache.enable = 0

I was working (zts-php CLI) with code included within a closure, and got really weird behaviour executing it right after i changed any code included there. The following executions worked fine, generating lots of confusion !

After disabling opcache, everything works as normal, every single time.

Infuse answered 24/12, 2022 at 11:57 Comment(0)
H
0

If anyone facing this issue in XAMPP at running Pear commands, then the problem can be found in the file pearcmd.php at line 446 within the function error_handler parameters.

function error_handler($errno, $errmsg, $file, $line, $vars)

A straightforward solution is to provide default value of null to $vars parameter like this

function error_handler($errno, $errmsg, $file, $line, $vars = null)

It will resolve the issue.

Hine answered 3/9, 2023 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.