code_junkie

Should I be extending this class? (PHP)

Nov 11th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. // Customer.class.php
  2. function getByName($name) {
  3. // SELECT * FROM `customer` WHERE `name` = $name
  4. }
  5.  
  6. // ** this could instead be written as: **
  7. // ORM.class.php
  8. function getByField($field, $value) {
  9. // SELECT * FROM `$this->table` WHERE `$field` = $value
  10. }
  11.  
  12. // Customer.class.php
  13. function getByName($name) {
  14. // SELECT * FROM `customer` WHERE `name` = $name
  15. }
  16.  
  17. // ** this could instead be written as: **
  18. // ORM.class.php
  19. function getByField($field, $value) {
  20. // SELECT * FROM `$this->table` WHERE `$field` = $value
  21. }
  22.  
  23. class Customer {
  24. public $name;
  25. public function save() {
  26. $orm = new ORM('customers', 'id'); // table name and primary key
  27. $orm->name = $this->name;
  28. $orm->save();
  29. }
  30. }
  31.  
  32. class Poll extends dbObject // dbObject is my ORM. Poll can extend it so it gets all properties.
  33. {
  34. function __construct($ID=false)
  35. {
  36. $this->__setupDatabase('polls', // db table
  37. array('ID_Poll' => 'ID', // db field => object property
  38. 'strPollQuestion' => 'strpollquestion',
  39. 'datPublished' => 'datpublished',
  40. 'datCloseDate' => 'datclosedate',
  41. 'enmClosed' => 'enmclosed',
  42. 'enmGoedgekeurd' => 'enmgoedgekeurd'),
  43. 'ID_Poll', // primary db key
  44. $ID); // primary key value
  45. $this->addRelation('Pollitem'); //Connect PollItem to Poll 1;1
  46. $this->addRelation('Pollvote', 'PollUser'); // connect pollVote via PollUser (many:many)
  47.  
  48.  
  49. }
  50.  
  51. function Display()
  52. {
  53.  
  54. // do your displayíng for poll here:
  55. $pollItems = $this->Find("PollItem"); // find all poll items
  56. $alreadyvoted = $this->Find("PollVote", array("IP"=>$_SERVER['REMOTE_ADDR'])); // find all votes for current ip
  57. }
Add Comment
Please, Sign In to add comment