我正在嘗試獲取所有行并將其設置為Inventory類。這是我的課程以及我如何獲取它class Inventory { public int $id; public string $name; public string $type_of; public float $weight;}$query = "SELECT * FROM inventories";$inventories = $this->db->prepare($query);$inventories->execute();$inventories = $inventories->fetchAll(PDO::FETCH_CLASS, 'Inventory');在數據庫中,該type_of屬性可以為空。因此,當我執行代碼時,它會拋出錯誤:類型化屬性 Inventory::$type_of 必須是字符串,使用 null有沒有辦法忽略 null 值并將其轉換為其類型默認值。示例:type_of是字符串,當值為null時會變成空字符串。weight是 float,那么它會變成0.0.太感謝了
使用嚴格屬性類型 PDO::FETCH_CLASS 時處理空值
慕碼人2483693
2023-07-01 17:34:02