[英]Removing unwanted data from class to save to the database
我有以下簡單的類:
class message {
public $message;
public $type;
public $timestamp;
private $types = [null, "success", "danger", "warning", "info", "primary"];
public function __construct(string $message, string $type = "success") {
$this->timestamp = time();
if(in_array(strtolower($type), $this->types)) {
$this->type = strtolower($type);
} else {
$this->type = null;
}
$this->message = $message;
}
}
所有這些消息都會從頁面給用戶響應,例如“成功保存到數據庫”等。
我想將消息保存到數據庫中,但是我不需要每次都保存private
屬性,因為這只是驗證消息類型以確保所有添加的消息都適合正確的可用類別。 我真的只想保存$message
、 $type
和$timestamp
。 我認為它會在數據庫中占用大量不必要的空間,每次都會有 50k 條消息和$type
設置。
我嘗試取消設置它,但它當然是私有財產。 這附近有什么好工作? 我應該將我的三個值放在一個數組中並完成它嗎? 還有其他解決方案嗎?
如果您序列化類使用 __serialize() ,則此方法返回自定義數組以進行序列化。 例子:
class test{
public $name ;
public $type ;
function __construct()
{
$this->name = "test";
$this->type = "type";
}
function __serialize()
{
return [$this->type];
}
}
在這個例子中$name
變量被跳過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.