krz/php-blog

A PHP blogging system with built-in static comments.

clone: git clone https://gitbay.org/krz/php-blog.git

main: _classes/Comment.php · raw

 1<?php
 2
 3
 4class Comment
 5{
 6    function __construct(string $comment, string $postURL, string $username = 'Anonymous')
 7    {
 8        $this->timestamp = date('Y-m-d H:i:s');
 9        $this->username = $username;
10        $this->comment = $comment;
11        $this->postURL = $postURL;
12    }
13
14    function saveComment(string $fileName)
15    {
16        if (file_exists($fileName)) {
17            $sourceData = file_get_contents($fileName);
18            $tempArray = json_decode($sourceData);
19            array_push($tempArray, $this);
20            $jsonData = json_encode($tempArray, JSON_PRETTY_PRINT);
21            file_put_contents($fileName, $jsonData);
22        } else {
23            die('Error: The ' . $fileName . ' file does not exist.');
24        }
25    }
26}