php - instatiating an object whose class does not have an __construct method
I'm writing a code for a login page which makes use of a Validation class. This Validation class inherits a class called Sanitize. Neither of the two classes has an __construct method.
However after instantiating a user object from the Validation Class and call the method "sanitize" i get a fatal error
Uncaught ArgumentCountError: Too few arguments to function Sanitize::sanitize(), 0 passed in C:\xampp\htdocs\onyialine\login.php on line 20 and exactly 1 expected in \onyialine\classes\Validation.php Stack trace: #0 C:\xampp\htdocs\onyialine\login.php(20): Sanitize->sanitize() #1 {main} thrown in C:\xampp\htdocs\onyialine\classes\Validation.php on line 6
But if I pass in an $input argument when instatiating the user object the code works as i want.
I'm confused and want to know the reason behind this, because neither of the two Classes has an __constructor method.
/**
*login.php
*/
<body>
<?php
if (isset($_POST['submit'])) {
$input = [
'email' => $_POST['email'],
'password1' => $_POST['password1']
];
print_r($input);
require './classes/Validation.php';
$user = new Validation(); //line 20
$user->sanitize($input);
$user->validate_login($input);
if(empty($user->errors)){
echo "we can process";
}else{
echo "we cannot process";
}
}
?>
<h1>login </h1>
<form action="login.php" method="post" noValidate>
<input type="email" name="email" placeholder="email">
<input type="password" name="password1" placeholder="password">
<input type="submit" name="submit" value="submit">
</form>
</body>
/*
*Validation.php
*/
<?php
class Sanitize{
function sanitize(array &$input){ // line 6
foreach($input as $key => $value){
switch ($key) {
case 'name':
$input[$key]= filter_var($value , FILTER_SANITIZE_STRING);
break;
case 'email':
$input[$key] = filter_var($value , FILTER_SANITIZE_EMAIL);
break;
case 'password1':
$input[$key] = filter_var($value , FILTER_SANITIZE_STRING);
break;
case 'password2':
$input[$key] = filter_var($value , FILTER_SANITIZE_STRING);
break;
}
}
}
}
class Validation extends Sanitize{
public $errors = [];
function validate_register(array $input){
if( empty($input['name']) || !(preg_match('/[a-z\s]/i', $input['name'])) || !(strlen($input['name']) <= 30)){
$this->errors[] = 'name missing or not alphabetic and space characters. Max 30';
}
if(empty($input['email']) || !( filter_var( $input['email'] , FILTER_VALIDATE_EMAIL) ) || (strlen($input['email']) > 60)){
$this->errors[] = 'You forgot to enter your email address or the e-mail format is incorrect or the length of the email has exceeded 60 chars';
}
if(empty($input['password1'])){
$this->errors[] = 'please enter a password';
}else{
if(!preg_match( '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#[email protected]!%&*?])[A-Za-z\d#[email protected]!%&*?]{8,12}$/',
$input['password1']))
{
$this->errors[] = 'Invalid password, 8 to 12 chars, one upper, one lower, one number, one special.';
}else{
if( $input['password1'] !== $input['password2']){
$this->errors[] = 'Your two password do not match.';
}
}
}
}
function validate_login(array $input){
if(empty($input['email']) || !( filter_var( $input['email'] , FILTER_VALIDATE_EMAIL) ) ){
$this->errors[] = 'You forgot to enter your email address or the e-mail format is incorrect';
}
if(empty($input['password1'])){
$this->errors[] = 'you forgot to enter your password';
}
}
}
?>
Answer
Solution:
The issue here is that you have named your method the same name as your class. This acts the same as
__construct()
methods would, and in previous PHP versions, does not show depreciated warnings.See a live demo of this conflicting.
Thus, when you call
new Validator()
, the parent class is trying to construct itself without any arguments. You need to rename the method, or the class, in order for this to stop clashing.For more information, refer to the PHP Docs.