Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> trait vs interface
kayman
post
Post #1





Grupa: Zarejestrowani
Postów: 556
Pomógł: 40
Dołączył: 20.07.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


może chcę za dużo pisząc takie?


  1.  
  2. trait Tr {
  3.  
  4. public function getC() {
  5. echo 'asd';
  6. }
  7.  
  8. }
  9. }
  10.  
  11.  
  12. interface In {
  13.  
  14. public function getC();
  15.  
  16. }
  17.  
  18.  
  19. class B extends A implements In {
  20.  
  21. public function getC() {
  22. echo 'asd';
  23. }
  24.  
  25. }
  26.  
  27.  
  28. // a to wywala błąd
  29.  
  30.  
  31. class B extends A implements In {
  32.  
  33. use Tr;
  34.  
  35.  
  36. }
  37.  
  38.  


czyżby interface nie rozpoznaje metod wstrzykniętych przez trait?

Ten post edytował kayman 26.01.2015, 12:33:41
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kayman
post
Post #2





Grupa: Zarejestrowani
Postów: 556
Pomógł: 40
Dołączył: 20.07.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


a teraz już zgłupiałem, co ma metoda z parenta wspólnego z interface?

Kod
Fatal error: Declaration of Framework\Models\Master\MasterModel::setData() must be compatible with Framework\Models\Master\ModelInerface::setData($id) in C:\xampp\htdocs\lpcms\Framework\Models\Master\TableUsers.php on line 18



  1.  
  2. interface ModelInerface {
  3.  
  4. public function __construct();
  5.  
  6. public function setData($id);
  7.  
  8. public function __set($key, $value);
  9.  
  10. public function __get($key);
  11.  
  12. public function getAll($order = null);
  13.  
  14. public function save();
  15.  
  16. public function getData($id = null);
  17.  
  18. public function delete($id);
  19.  
  20. }
  21.  
  22. trait ModelTraits {
  23.  
  24.  
  25. public function setData($id) {
  26. parent::setData($this->table, $this->columns, $id);
  27. }
  28.  
  29. public function __set($key, $value) {
  30. $this->set($this->table, $key, $value);
  31. }
  32.  
  33. public function __get($key) {
  34. return $this->get($this->table, $key);
  35. }
  36.  
  37. public function getAll($order = null) {
  38. return parent::getAll($this->table, $this->columns, $order);
  39. }
  40.  
  41. public function save() {
  42. parent::save($this->table);
  43. }
  44.  
  45. public function getData($id = null) {
  46. if ($id)
  47. $this->setData($id);
  48. return parent::getData($this->table);
  49. }
  50.  
  51. public function delete($id) {
  52. parent::delete($this->table, $id);
  53. }
  54.  
  55. }
  56.  
  57.  
  58. class TableUsers extends MasterModel implements ModelInerface {
  59.  
  60. private $table = 'users';
  61. private $columns = array('id', 'mail', 'pass', 'level', 'firstname', 'lastname', 'lang');
  62.  
  63. public function __construct() {
  64. parent::__construct($this->table, $this->columns);
  65. }
  66.  
  67. use ModelTraits;
  68.  
  69. // cut
  70.  
  71. }
  72.  
  73. class MasterModel {
  74.  
  75. public $data;
  76.  
  77. public function __construct($table, $columns) {
  78. foreach ($columns as $value) {
  79. $this->data[$table][$value] = null;
  80. }
  81. }
  82.  
  83. public function setData($table, $columns, $id) {
  84. $query = new QueryBuider();
  85. $query->setColumns($columns);
  86. $query->setFrom($table);
  87. $query->addWhere('id', $id);
  88. $sql = \Framework\Libs\Sql::instance();
  89. $data = $sql->getSingleRow($query->getSelect());
  90. foreach ($columns as $value) {
  91. $this->data[$table][$value] = $data[$value];
  92. }
  93. }
  94.  
  95. // cut
  96.  
  97. }
  98.  
  99.  

Go to the top of the page
+Quote Post

Posty w temacie
- kayman   trait vs interface   26.01.2015, 12:32:21
- - Crozin   Cytata to wywala błądNie uważasz, że informacja ja...   26.01.2015, 12:35:05
- - kayman   jak jest [PHP] pobierz, plaintext  class B ...   26.01.2015, 12:39:56
- - Pyton_000   [PHP] pobierz, plaintext <?php trait Tr ...   26.01.2015, 12:45:51
- - Crozin   [PHP] pobierz, plaintext <?php interface I...   26.01.2015, 12:46:55
- - kayman   a teraz już zgłupiałem, co ma metoda z parenta wsp...   26.01.2015, 13:22:37
- - Pyton_000   To że deklarujesz kontrakt (interfejs) z jednym pa...   26.01.2015, 14:02:30
- - Crozin   Masz niekompatybilne sygnatury metod. Nie ma to ża...   26.01.2015, 14:04:21
- - kayman   czyli to jest żle i trzeba by np. inaczej nazwać m...   26.01.2015, 14:17:31
- - Crozin   1. Wykorzystanie magicznego __get() jest raczej zł...   26.01.2015, 14:49:14
- - kayman   chciałem np takie coś [PHP] pobierz, plaintext ...   26.01.2015, 15:35:07
- - Pyton_000   setData możesz spokojnie wywalić i dać w konstrukt...   26.01.2015, 15:48:51
|- - kayman   Cytat(Pyton_000 @ 26.01.2015, 15:48:5...   26.01.2015, 16:03:47
- - Pyton_000   ihmo [PHP] pobierz, plaintext $page = new Page...   26.01.2015, 16:09:26
- - kayman   dzięki panowie, wiem mniej więcej jak to przebudow...   27.01.2015, 09:23:57
- - Crozin   1. Takie podejście, czyli tzw. ActiveRecord, jest ...   27.01.2015, 10:37:36
- - Pyton_000   Może też użyć lżejszego Eloquent z Laravela, ale n...   27.01.2015, 10:55:31
- - kayman   nie mam nic przeciwko ORM z prawdziwego zdarzenia ...   27.01.2015, 11:27:18
- - Pyton_000   W Twoim Przypadku Trait nie jest potrzebny Bo: - i...   27.01.2015, 11:31:24
- - kayman   Pyton masz racje tylko w ten sposób zaraz napiszę ...   27.01.2015, 11:48:54
- - Pyton_000   Jeżeli chcesz się czegoś nauczyć to możesz rozwija...   27.01.2015, 11:55:36


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 16.10.2025 - 14:01