Fatal error when creating a class

Hi,
I created this class to save sign-up data into database (signup.php):

<?php

class Signup

{

    public function RegisterNewUser()

    {

        if (isset($_POST['submit_form'])) {

            $this->RegisterToDatabase();

        }

    }

    

    public function RegisterToDatabase()

    {

        require "../app/core/database.php";

        $name = $_POST['first_name'];

        $last_name = $_POST['last_name'];

        $birthday = $_POST['birthday'];

        $gender = $_POST['gender'];

        $email = $_POST['email'];

        $phone = $_POST['phone'];

        $sql = "INSERT INTO signup (FirstName, LastName, Birthday, Gender, Email, Phone)

         VALUES ('$name', '$last_name', '$birthday', '$gender', '$email', '$phone')";

        $connection->query($sql);

    }

}

and used the following code to instantiate that class (index.php):

<?php

require_once "../app/model/signup.php";

$signup = new Signup();

?>

The error says:

Fatal error : Cannot declare class Signup, because the name is already in use in C:\xampp\htdocs\MyProject\app\model\signup.php on line 3

How to fix it?

It’s better not to put $_POST inside a class, but you could do it that way. The better way to do it would to do something like the following:

if (($_SERVER['REQUEST_METHOD'] === 'POST') && isset($_POST['submit'])) {

    $send = new sendMail();
    $data = [];
    $data['name'] = $_POST['user']['first_name'] . ' ' . $_POST['user']['last_name'];
    $data['validation'] = $send->validationFCN(20);
    $_POST['user']['validation'] = $data['validation'];

    $data['email'] = $_POST['user']['email'];
    $data['phone'] = $_POST['user']['phone'];
    $data['birthday'] = $_POST['user']['birthday'];

    $data['message'] =
        '<html lang="en">' .
        '<body style=\'background: #eee;\'>' .
        '<p style="font-size: 1.8em; line-height: 1.5;">Full Name : ' . $data['name'] .
        '<br>Email Address : ' . $data['email'] .
        '<br>Phone : ' . $data['phone'] .
        '<br>Birthday : ' . $data['birthday'] . '</p>' .
        '<p style="font-size: 1.4em; line-height: 1.5;">Please click on link: https://www.phototechguru.com/admin/activate.php?confirmation=' . $data['validation'] . ' in order to have access to the Miniature Photographer Website.</p>' .
        '<p style="font-size: 1.4em; line-height: 1.5;">In addition please answer the question "Meet Me Under the [blank] Clock" with the name of the clock in the image that was sent.</p>' .
        '</body>' .
        '</html>';



    $register = new Register($_POST['user']);
    $result = $register->create();
    if ($result) {
        $send->verificationEmail($data);
        header("Location: success.php");
        exit();
    }

    header("Location: register.php");
    exit();
}

The goal is to be able to use PHP Classes to your advantage and benifit.

The reason you are getting an error is you are naming variables/classes/whathaveyou the same AND/OR you are NOT loading your class(es) in - (I think, not loading them in?). I’m using a composer autoloader to load my clases:

I recommend autoloading classes (though you can do it manually) and using namespaces that way you don’t have to worry about using the same names. Here’s just one link that I found on autoloading - https://code.tutsplus.com/tutorials/how-to-autoload-classes-with-composer-in-php–cms-35649

require_once "../assets/config/config.php"; // PHP Configurations and stuff:

require_once "../vendor/autoload.php"; // Composer Autoloader

use PhotoTech\Register; // Namespace for that particular class (PhotoTech)

use PhotoTech\sendMail;

you could even do the following

use PhotoTech\Register as Signup;

the class

namespace PhotoTech;


    class Register extends DatabaseObject
    {
        protected static string $table = "admins"; // Table Name:
        static protected array $db_columns = ['first_name', 'last_name', 'email', 'username', 'phone', 'security', 'hashed_password', 'validation', 'new_date', 'update_date', 'birthday'];

Hope it helps:

1 Like

It means the file is being included more than once. Rather than manually including the file, look into composer and register the class. Then, whenever it’s needed it will be found. It will also help with more modern PHP writing.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service