Advertisement
xenoside

trait example

Sep 17th, 2024
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.70 KB | None | 0 0
  1. <?php
  2.  
  3. trait DatabaseInsert {
  4.     public function insert() {
  5.         echo "{$this->getDb()} insert\n";
  6.     }
  7. }
  8.  
  9. trait DatabaseUpdate {
  10.     public function update() {
  11.         echo "{$this->getDb()} update\n";
  12.     }
  13. }
  14.  
  15. trait DatabaseDelete {
  16.     public function delete() {
  17.         echo "{$this->getDb()} delete\n";
  18.     }
  19. }
  20.  
  21. class Database {
  22.     private $db = null;
  23.     public function __construct() {
  24.         $this->db = 'DB Resource';
  25.     }
  26.     public function getDb() {
  27.         return $this->db;
  28.     }
  29. }
  30.  
  31. class OnlyInsert extends Database {
  32.     use DatabaseInsert;
  33. }
  34.  
  35. $db = new OnlyInsert();
  36. $db->insert(); // DB Resource insert
  37. $db->update(); // PHP Fatal error:  Uncaught Error: Call to undefined method OnlyInsert::update() in test.php:37
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement