php - Outputting form data via classes

531

I'm trying to figure out how to output data from a form using classes. I've validated my form data in a validation PHP file and i'm using another file called database to output the text from my form to a file. I've tried using the function 'init' with multiple arguments: resulted in a blank file. Tried to hardcode the data: also blank.

class Database implements JsonSerializable
{
    public $name;
    public $age;
    public $email;
    public $phone;
    public $file = "database.txt";
    private $filehandler;
    public function __construct($name, $age, $email, $phone)
    {
        $this -> name = $name;
        $this -> age = $age;
        $this -> email = $email;
        $this -> phone = $phone;
    }
    public function init(){
        $this -> filehandler = fopen($this -> file, "w");
        fwrite($this -> file -> $this->filehandler, "Help");
        fclose($this -> filehandler);
    }
    public function jsonSerialize()
    {
       return ['name' => $this -> name,
                'age' => $this -> age,
                'email' => $this -> email,
                'phone' => $this -> phone
       ];
    }
}

Object creation:

$database = new Database($_POST['name'], $_POST['age'], $_POST['email'], $_POST['phone']);

$database->jsonSerialize();
$database->init();
924

Answer

Solution:

Change

        fwrite($this -> file -> $this->filehandler, "Help");

to

        fwrite($this -> filehandler, "Help");

Full code

<?php

class Database implements JsonSerializable
{
    public $name;
    public $age;
    public $email;
    public $phone;
    public $file = "database.txt";
    private $filehandler;

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

    public function init()
    {
        $this->filehandler = fopen($this->file, "w");
        fwrite($this->filehandler, $this->jsonSerialize());
        fclose($this->filehandler);
    }

    public function jsonSerialize()
    {
        return json_encode(
            [
                'name'      => $this->name,
                'age'       => $this->age,
                'email'     => $this->email,
                'phone'     => $this->phone
            ]
        );
    }
}

// $database = new Database($_POST['name'], $_POST['age'], $_POST['email'], $_POST['phone']);
$database = new Database('test', 100, '[email protected]', 999);
$database->init();

People are also looking for solutions to the problem: php - Bootstrap with Webpack Encore functions with the internal webserver of Symfony 4 but not with LAMP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.