I'm trying to convert my procedural login to OOP but I get ArgumentCountError. This is a project for my homework, can you help me?
Down below is my php code with class and login function and HTML Login page with small php script.
DB Connection is PDO.
When i going to istance my php function with "$login = new Login; $login->login($email,$password,$type);" i get this error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Login::login(), 0 passed in C:\xampp\htdocs\public_html\project OOP\include\login.php on line 50 and exactly 3 expected in C:\xampp\htdocs\public_html\project OOP\include\login.php:8 Stack trace: #0 C:\xampp\htdocs\public_html\project OOP\include\login.php(50): Login->login() #1 {main} thrown in C:\xampp\htdocs\public_html\project OOP\include\login.php on line 8.
I know that there are a lot of mistakes here, but I'm a beginner.
class Login{
private $email;
private $password;
private $type;
public function login($email,$password,$type){
if(!empty($_POST['email']) && !empty($_POST['password'])&& !empty($_POST['type'])):
switch ($_POST['type']) {
case 'Buyer':
$records = $conn->prepare('SELECT * FROM buyer WHERE email = :email');
break;
case 'Seller':
$records = $conn->prepare('SELECT * FROM seller WHERE email = :email');
break;
default:
break;
}
$_SESSION['type'] = $_POST['type'];
$records->bindParam('email', $_POST['email']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
$message = '';
if($results && count($results) > 0 && md5($_POST['password'], $results['password'])){
$_SESSION['user_id'] = $results['buyer_id'];
$_SESSION['name'] = $results['buyer_name'];
$_SESSION['email'] = $results['email'];
$_SESSION['password'] = $results['password'];
$_SESSION['phone'] = $results['phone'];
$_SESSION['description'] = $results['description'];
$_SESSION['status'] = $results['status'];
header("Location: index.php");
} else {
$message = 'Sorry, those credentials do not match';
}
endif;
}
}
This is only for test
$email = "[email protected]";
$password = "123456";
$type = "Buyer";
$login = new Login;
$login->login($email,$password,$type);
My HTML code:
<?php
session_start();
if ( isset($_SESSION['user_id']) ){
header("Location: index.php");
}
require 'database.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login V1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
<link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="css/util.css">
<link rel="stylesheet" type="text/css" href="css/login.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ width: 350px; padding: 20px; }
</style>
<!--===============================================================================================-->
</head>
<body>
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100">
<div class="login100-pic js-tilt" data-tilt>
<img src="images/img-01.png" alt="IMG">
</div>
<form class="login100-form validate-form" action="login.php" method="POST">
<span class="login100-form-title">
Member Login
</span>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: [email protected]">
<input class="input100" type="text" name="email" placeholder="Email">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-envelope" aria-hidden="true"></i>
</span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Password is required">
<input class="input100" type="password" name="password" placeholder="Password">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-lock" aria-hidden="true"></i>
</span>
</div>
<select class="browser-default custom-select" name="type">
<option disabled="disabled" selected="selected">Profile type</option>
<option>Buyer</option>
<option>Seller</option>
</select>
<div class="container-login100-form-btn">
<button class="login100-form-btn" type="submit" name="submit">
Login
</button>
</div>
<div class="text-center p-t-12">
<span class="txt1">
Forgot
</span>
<a class="txt2" href="#">
email / Password?
</a>
</div>
<div class="text-center p-t-136">
<a class="txt2" href="register.php">
Create your Account
<i class="fa fa-long-arrow-right m-l-5" aria-hidden="true"></i>
</a>
</div>
</form>
</div>
</div>
</div>
<!--===============================================================================================-->
<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/bootstrap/js/popper.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
<script src="vendor/tilt/tilt.jquery.min.js"></script>
<script >
$('.js-tilt').tilt({
scale: 1.1
})
</script>
<!--===============================================================================================-->
<script src="js/main.js"></script>
</body>
</html>
Your login
function is being interpreted as a constructor because it has the same name as the class. (See PHP 4 Style Constructors.)
You must not be seeing the deprecation message because of your error reporting settings.
The deprecation isn't the problem, though. It ends up turning into a fatal error because
$login = new Login;
calls the login method with no arguments, since PHP treats it like a constructor.
You don't need a __construct() method though, because that function isn't actually supposed to be a constructor. You can just change the name of the class or the method, that should fix the error.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加