Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Korzystanie z parametrów klasy extends
Forum PHP.pl > Forum > Przedszkole
vegeta
Witam.

Mam klasę rodzica, która z bazy pobiera mi dane o użytkowniku:

  1. <?php
  2. class MY_Controller extends CI_Controller {
  3.  
  4. public function _check_login()
  5. {
  6. if ($cookie = $this->input->cookie('cookie', TRUE))
  7. {
  8. $dane = unserialize(base64_decode($cookie));
  9. if (is_array($dane))
  10. {
  11. $ar = $this->User->get_user_by_login($dane['user'])->result_array();
  12. if (isset($ar[0]) and $ar[0]['user_last_login_ip'] == $this->input->ip_address() and sha1(md5($ar[0]['user_login'].$ar[0]['user_password'])) == $dane['pass'])
  13. {
  14. return true;
  15. }
  16. else
  17. {
  18. return false;
  19. }
  20. }
  21. else
  22. {
  23. return false;
  24. }
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31.  
  32. public function _check_perm($perm)
  33. {
  34. if ($cookie = $this->input->cookie('cookie', TRUE))
  35. {
  36. $dane = unserialize(base64_decode($cookie));
  37. if (is_array($dane))
  38. {
  39. $ar = $this->User->get_user_by_login($dane['user'])->result_array();
  40. if (isset($ar[0]) and $ar[0]['user_last_login_ip'] == $this->input->ip_address() and sha1(md5($ar[0]['user_login'].$ar[0]['user_password'])) == $dane['pass'])
  41. {
  42. if ($ar[0]['user_perms'] == $perm)
  43. {
  44. return true;
  45. }
  46. else
  47. {
  48. return false;
  49. }
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66. }


Funkcje z tego rodzica wywołuję w klasie tak:
  1. <?php
  2. class Owned extends MY_Controller
  3. (...)
  4. if (!MY_Controller::_check_login())


Ale później muszę pobierać znowu dane z bazy o userze, ale w kontrolerze:

  1. $this->cookie = unserialize(base64_decode($this->input->cookie('cookie', TRUE)));
  2. $this->user = $this->User->get_user_by_login($this->cookie['user'])->result_array();


I tutaj mam pytania:

1. Jak zrobić, abym mógł się odwołać do już pobranych danych w rodzicu MY_Controller? (zeby nie pisac kodu, ktory podalem jako ostatni)
2. Czy można to bardziej zoptymalizować?
3. Jeśli macie inne uwagi co do mojego kodu to piszcie
CuteOne
Brzydki ten kod smile.gif

  1.  
  2. function aaa() {
  3.  
  4. if(1 == 2) {
  5.  
  6. if(2==1) {
  7.  
  8. return true;
  9. }
  10. }
  11.  
  12. return false;
  13. }


zamiast naparzać falsami wystarczy dać jeden na końcu. Co do pytania

  1.  
  2. public function aa() {
  3.  
  4. if(1 == 1) {
  5.  
  6. $this->ok = 'aaaa';
  7. return true;
  8. }
  9. return false;
  10. }
  11.  
  12. if(!$this->aa()) {
  13.  
  14. }
  15. else {
  16.  
  17. echo $this->ok;
  18. }


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.