php - Outputting form data via classes
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();
Answer
Solution:
Change
to
Full code