03-database_item.php 816 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace cx_newsletter;
  3. abstract class database_item
  4. implements database_item_interface {
  5. public function __construct(?int $id = null) {
  6. $this->id = $id;
  7. }
  8. public function has_id() : bool {
  9. return $this->id !== null;
  10. }
  11. public function get_id() : int {
  12. if (!$this->has_id()) {
  13. $exception = "\n\n";
  14. $exception .= 'Database item which ID trying to be loaded, has ';
  15. $exception .= 'no ID. That mean it could not exists, in the ';
  16. $exception .= 'database, because only items which not exists in ';
  17. $exception .= 'the database has not own ID.';
  18. $exception .= "\n\n";
  19. throw new \exception($exception);
  20. }
  21. return $this->id;
  22. }
  23. private ?int $id;
  24. }