Tiếp theo cho Singleton Design Patterns trong PHP. Mình tiếp tục chia sẻ với mọi người về viết tạo class Logs bằng các sử dụng Singleton Design Patterns. Về Singleton thì mình có chia sẻ ở bài viết trước , bạn có thể xem lại lại đây:
Connect to Database using Singleton Design Patterns with PHP
Github: https://github.com/skipperhoa/Design-Patterns
Okay, giờ mình tạo class Logger theo kiểu Singleton
<?php
class Logger {
private static $instance;
private $logs;
private function __construct() {
$this->logs = [];
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Logger();
}
return self::$instance;
}
public function log($message) {
$this->logs[] = $message;
}
public function getLogs() {
return $this->logs;
}
}
$logger = Logger::getInstance();
$logger->log("Log message 1</br>");
$logger->log("Log message 2</br>");
$logs = $logger->getLogs();
foreach ($logs as $log) {
echo $log . "\n";
}
?>
Hoặc cách dùng sau:
<?php
class Logger
{
// Đây là biến tĩnh, dùng để lưu giữ instance duy nhất của lớp Logger
private static $instance = null;
// thuộc tính để lưu giữ đường dẫn đến file log
private $logFile;
// Make the constructor private to prevent instantiation
private function __construct()
{
// Đường dẫn file log (app.log)
$this->logFile = __DIR__ . '/app.log';
// ghi một header "---- Log Start ----" vào đầu file log (chỉ khi file chưa có nội dung)
// Kiểm tra nếu file log trống hoặc không tồn tại thì mới ghi header
if (!file_exists($this->logFile) || filesize($this->logFile) === 0) {
file_put_contents($this->logFile, "---- Log Start ----\n", FILE_APPEND);
}
}
// Method to get the single instance of the Logger
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new Logger();
}
return self::$instance;
}
// Write a message to the log file
public function log($message)
{
$timestamp = date('Y-m-d H:i:s');
$formattedMessage = "[{$timestamp}] - {$message}\n";
// Append the message to the log file
file_put_contents($this->logFile, $formattedMessage, FILE_APPEND);
}
// Prevent cloning
private function __clone() {}
// Prevent unserialization
private function __wakeup() {}
}
//__clone() và __wakeup() là các phương thức "magic" (phương thức đặc biệt) được PHP cung cấp sẵn
// Usage
$logger = Logger::getInstance();
$logger->log("Application started");
$anotherLogger = Logger::getInstance();
$anotherLogger->log("Another part of the application logs a message");
// Verify both instances are the same
var_dump($logger === $anotherLogger); // true
// Check the log file (app.log) to see the output