0

При выполнении данного кода выходит ошибка(изображение) В функциях: public function signupUser() private function emptyInput() и в других проверяющих функциях.

Ошибка в классе-контроллере

signup.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="../assets/styles/authStyle.css">
    <title>Sign up</title>
</head>
<body>
    <div class="container">
        <section class="registration">
            <form action="../includes/signup.inc.php" method="POST">
                <h2>Регистрация</h2>
                <div>
                    <label for="nameId">Никнейм:</label>
                    <input type="text" name="name" id="nameId">
                </div>
                <div>
                    <label for="passwordId">Пароль:</label>
                    <input type="password" name="password" id="passwordId">
                </div>
                <div>
                    <label for="passwordId">Повторите пароль:</label>
                    <input type="password" name="confirmPassword" id="passwordId">
                </div>
                <div>
                    <label for="emailId">E-mail адрес:</label>
                    <input type="email" name="email" id="emailId">
                    <button type="submit">Подтвердить</button>
                </div>
            </form>
        </section>
    </div>
</body>
</html>

signup-controller.classes.php

<?php

class SignupController extends Signup
{
    private $name;
    private $password;
    private $confirmPassword;
    private $email;

    public function __construct($name, $password, $confirmPassword, $email)
    {
        $this->$name = $name;
        $this->$password = $password;
        $this->$confirmPassword = $confirmPassword;
        $this->$email = $email;
    }

    public function signupUser() 
    {
        if($this->emptyInput() == false) {
            //header("Location: ../auth/signup.php");
            //exit();
        }
        if($this->invalidName() == false) {
            header("Location: ../auth/signup.php");
            exit();
        }
        if($this->invalidEmail() == false) {
            header("Location: ../auth/signup.php");
            exit();
        }
        if($this->invalidPassowrd() == false) {
            header("Location: ../auth/signup.php");
            exit();
        }
        if($this->isUser() == false) {
            header("Location: ../auth/signup.php");
            exit();
        }

        $this->setUser($this->name, $this->password, $this->email);

    }

    private function emptyInput() 
    {
        if(empty($this->name) || empty($this->password) || empty($this->confirmPassword) || empty($this->email)) {
            $result = false;
            throw new Exception("empty input");
        } else {$result = true;}
        return $result;
    }

    private function invalidName() {
        if(!preg_match('|^[A-Z0-9]+$|i', $this->name)) {
            $result = false; 
            throw new Exception("invalid name");
        }
        else {$result = true;}
        return $result;
    }

    private function invalidEmail() {
        if(!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
            $result = false;
            throw new Exception("invalid email");
        }
        else {$result = true;}
        return $result;
    }

    private function invalidPassowrd() {
        if($this->password !== $this->confirmPassword) {
            $result = false;
            throw new Exception("not match");
        } else if (strlen($this->password) < 8) {
            throw new Exception("password is too short, need 8 symbols");
            $result = false;
        } else if (strlen($this->password) > 256) {
            throw new Exception("password is too long, need less then 256 symbols");
            $result = false;
        } else {$result = true;}
        return $result;
    }

    private function isUser() {
        if (!$this->checkUser($this->name, $this->email)) {
            $result = false;
            throw new Exception("User had already been created");
        } 
        else {$result = true;}
        return $result;
    }
}
3
  • ну и что тут не ясного? ты сам же выкидываешь эксепшн при пустом вводе 5 мая 2022 в 17:41
  • Можешь пожалуйста пояснить подробнее? Я только обучаюсь. Я ввёл все поля в форме, но exception по условию должен выкидываться когда result должен быть ложным. У меня exception появляется всё время, даже если все условия выполнены.
    – void
    5 мая 2022 в 17:45
  • 1
    отладка тебе в помощь ↑↑↑ ты будешь часто в процессе работы ею пользоваться... смотри что где и как у тебя происходит, заполняется и т.д. на каждом шаге что у тебя имеется 5 мая 2022 в 17:46

1 ответ 1

0

В конструкторе по всей видимости эти строки:

        $this->$name = $name;
        $this->$password = $password;
        $this->$confirmPassword = $confirmPassword;
        $this->$email = $email;

Нужно заменить на эти:

        $this->name = $name;
        $this->password = $password;
        $this->confirmPassword = $confirmPassword;
        $this->email = $email;

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.