Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Rozszerzenie klasy o funkcje
Forum PHP.pl > Forum > Przedszkole
arzach
Jak połączyć klasę SQL z klasą Eps tak by metody z klasy SQL wykonywały zapytania z klasy eps czyli

update_point() i delete_point()

  1. $user_point = 2;
  2.  
  3. class sql { //dopisujemy parę funkcji do klasy Eps
  4.  
  5. public function update(Eps $update) {
  6. return parent::mysql_query($update->update_point());
  7. }
  8.  
  9. public function remove(Eps $remove) {
  10. return parent::mysql_query($remove->delete_point());
  11. }
  12. }
  13.  
  14.  
  15. abstract class Eps_Cat_Point {
  16. protected $eps_cat_point = array('A' => 3, 'C' => 5, 'D' => 2);
  17.  
  18. // Dane beda pochodzic z bazy
  19. // Litera to identyfikator kategorii za co dostaje sie pkt np A za artykul 3 pkt.
  20.  
  21. }
  22.  
  23. class Eps extends Eps_Cat_Point {
  24.  
  25. public $user_id;
  26. public $cat_point;
  27. public $query;
  28. public $method_name;
  29.  
  30. public function __construct($user_id, $cat_point, $method_name) {
  31.  
  32. if(is_int($user_id) && preg_match("/^[A-Z]{1}$/", $cat_point) && is_string($method_name)){
  33. $this->user_id = $user_id;
  34. $this->cat_point = $cat_point;
  35.  
  36. if(method_exists(__CLASS__, $method_name)){
  37. $this->method_name = $method_name;
  38.  
  39.  
  40. ///$sql = new sql($this); jak tutaj dodac kod z klasy sql
  41.  
  42.  
  43. call_user_func(array(__CLASS__, $this->method_name));
  44.  
  45. }
  46.  
  47. } else {
  48. throw new Exception("Niepoprawna wartosc podanych parametrow");
  49. }
  50.  
  51. }
  52.  
  53. public function check_point(){
  54.  
  55. if($user_point < $this->eps_cat_point[$this->cat_point]){
  56. return true;
  57. } else {
  58. return false;
  59. }
  60.  
  61. }
  62.  
  63. public function update_point(){
  64. $this->query = "UPDATE tabela SET user_point=user_point+'".$this->eps_cat_point[$this->cat_point]."' WHERE user_id='".$this->user_id."'";
  65. return $this->query;
  66. }
  67.  
  68. public function delete_point(){
  69.  
  70. if($this->check_point()){
  71. $this->query = "UPDATE tabela SET user_point=0 WHERE user_id='".$this->user_id."'";
  72. return $this->query;
  73. } else {
  74. $this->query = "UPDATE tabela SET user_point=user_point-'".$this->eps_cat_point[$this->cat_point]."' WHERE user_id='".$this->user_id."'";
  75. return $this->query;
  76. }
  77. }
  78.  
  79. }
  80.  
  81.  
  82. $eps = new Eps(12, 'A', 'update_point');



.
Daimos
  1. class sql extends eps {
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.