PHP does not recognize connection as parameter

Asked

Viewed 156 times

0

I’m making a system using MVC (first time I use it in PHP), and when I will login appears:

Uncaught Argumentcounterror: Too few Arguments to Function Login::login(), 2 passed in C: wamp64 www php_pdo controller logincontroller.php on line 19 and Exactly 3 expected in C: wamp64 www php_pdo model login.php

Too few Arguments to Function Login::login(), 2 passed in C: wamp64 www php_pdo controller logincontroller.php on line 19 and Exactly 3 expected in C: wamp64 www php_pdo model login.php on line 29

In case the error says a parameter is missing, I tested the post of the login, and is going the 2 values (password and user), and conexãO is going too, but he still presents these mistakes..

Codes:

View: Sign-in.php:

    <form method="post" action="" >
        <input type="text" name="usuario" placeholder="Digite seu nome de usuário"><br>
        <input type="password" name="senha" placeholder="Digite sua senha"><br>
        <input type="submit" name="btn" value="Entrar"><br>
    </form>   
    
    <?php
    // caso haja post, esse if será executado
    if($_POST){
        // Aqui ele vai filtar o post, o ultimo parametro serve para tirar tags e caracteres especiais.
        $usuario = filter_input(INPUT_POST, 'usuario', FILTER_SANITIZE_STRING);
        $senha = filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING);
        // aqui ele inclui a classe
        require_once '../controller/logincontroller.php';
        // aqui ele chama o método Login, por ser static não precisa criar um objeto.
        LoginController::logar($usuario, $senha);
        
    } 

Controller logincontroller.php:

require_once '../model/login.php';

class LoginController {

 static function logar($usuario, $senha){
    $result = Login::login($usuario, $senha);
    if ($result){
        echo '<script> confirm("Bem vindo ao sistema!");
                       window.location("menu"); <script>';
            // header("Location: menu");
    } else {
        echo '<script> confirm("Senha ou usuário incorreto!");
                       window.location("sign-in"); <script>';
    }
 }

}

Model login.php:

require_once '../projeto/connectionfactory.php';

class Login {

private $usuario;
private $senha;
private $con;

function __construct($usuario, $senha) {
    $this->usuario = $usuario;
    $this->senha = $senha;
    $this->con = ConnectionFactory::getConnection();
}

static function login($usuario, $senha, $con) {
    // isso é para tratar erros
    try {
        // aqui cria a transação, tudo que for feito dentro, fica protegido
        $con->beginTransaction();
        // puxar senha
        $stmt = $con->prepare("select senha from tbl_login where usuario = :usuario");
        $stmt->bindParam('usuario', $usuario);
        $stmt->execute();
        $hash = null;
        while ($obj = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $hash = $obj['senha'];
        }
        if (password_verify($senha, $hash)) {
            return true;
        } else {
            return false;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
}

Project connectionfactory.php:

class ConnectionFactory{
static function getConnection(){
    $db = 'tec_find';
    $user = 'root';
    $psw = '';
    try {
        $con = new PDO("mysql:host=localhost;dbname=$db", $user, $psw); 
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $con;
    } catch (Exception $ex) {
        echo $ex->getMessage();
    }   
}
static function testarConnection(){
    if(ConnectionFactory::getConnection()){
        // entra sempre aqui
        echo 'Conectado';
    } else { 
        echo 'Conectado';
    }
    
}
}

pulp structure:

Estrutura

2 answers

2


Leandro good night.

You created a class with a __Construct and a static method, and in this __contruct you passed your connection, but there’s a problem, when we use the static method we don’t instantiate the class, so __Construct is without reason because it exists there.

If you want to keep doing it this way, try it this way.

require_once '../projeto/connectionfactory.php';

class Login {

private $usuario;
private $senha;

public function __construct($usuario, $senha) {
    $this->usuario = $usuario;
    $this->senha = $senha;
}

public static function login($usuario, $senha) {
     $con = ConnectionFactory::getConnection();
    // isso é para tratar erros
    try {
        // aqui cria a transação, tudo que for feito dentro, fica protegido
        $con->beginTransaction();
        // puxar senha
        $stmt = $con->prepare("select senha from tbl_login where usuario = :usuario");
        $stmt->bindParam('usuario', $usuario);
        $stmt->execute();
        $hash = null;
        while ($obj = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $hash = $obj['senha'];
        }
        if (password_verify($senha, $hash)) {
            return true;
        } else {
            return false;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
}
  • Thanks, I didn’t even notice that.

0

@Leandro is actually accusing that only 2 arguments were passed and the expected are 3, you are calling the function Login::login thus:

$result = Login::login($usuario, $senha);

But it’s defined like this:

static function login($usuario, $senha, $con) {

Try it this way:

$result = Login::login($usuario, $senha, $this->con);
  • Posted with the same error :/

  • If I take the $con from the parameter, and within the Login put $this->con->, it still shows the same error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.