Guest User

Untitled

a guest
May 29th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. class PDODO
  6. {
  7. protected $table_name;
  8. private $db;
  9.  
  10. /**
  11. *
  12. * @Connect to the database and set the error mode to Exception
  13. *
  14. * @Throws PDOException on failure
  15. *
  16. */
  17. public function conn()
  18. {
  19. if (!$this->db instanceof PDO)
  20. {
  21. $this->db = new PDO($this->dsn, $this->username, $this->password);
  22. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  23. }
  24. }
  25.  
  26. /**
  27. *
  28. * The constructor each derived class should have
  29. * its own constructor containing proper values
  30. *
  31. */
  32. function __construct()
  33. {
  34. $this->dsn = "mysql:dbname=test;host=localhost";
  35. $this->username = 'username';
  36. $this->password = 'password';
  37.  
  38. $this->table_name = get_called_class();
  39. }
  40.  
  41.  
  42. /**
  43. * settor
  44. *
  45. * @access public
  46. *
  47. * @param string $name
  48. *
  49. * @param string $value
  50. *
  51. */
  52. public function __set($name, $value)
  53. {
  54. switch($name)
  55. {
  56. case 'dsn':
  57. $this->dsn = $value;
  58. break;
  59.  
  60. case 'username':
  61. $this->username = $value;
  62. break;
  63.  
  64. case 'password':
  65. $this->password = $value;
  66. break;
  67.  
  68. case 'field_list':
  69. if(is_array($value))
  70. {
  71. $this->field_list = $value;
  72. }
  73. break;
  74.  
  75. case 'primary_key':
  76. $this->primary_key = $value;
  77. break;
  78.  
  79. default:
  80. throw new Exception ("Uable to set $name");
  81. }
  82. }
  83.  
  84.  
  85.  
  86. public function select($id=null)
  87. {
  88. }
  89.  
  90.  
  91.  
  92. public function insert($values)
  93. {
  94. }
  95.  
  96.  
  97. public function update($id, $values)
  98. {
  99. }
  100.  
  101.  
  102. } // end of class
  103.  
  104.  
  105. /**
  106. *
  107. * @class name is named the same as the db table
  108. *
  109. */
  110. class cars extends PDODO
  111. {
  112. protected $car_id, $car_cost, $car_price, $car_description;
  113.  
  114. } // end class
  115.  
  116.  
  117. $cars = new cars;
  118.  
  119. $cars->select(3);
  120. ?>
Add Comment
Please, Sign In to add comment