Moja pierwsza klasa - rejestr |
Moja pierwsza klasa - rejestr |
14.02.2016, 12:52:24
Post
#1
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
Czesc wszystkim,
Poznaje PHP i w ramach nauki napisalem swoja 1 klase do obslugi rejestru. Bede wdzieczny za wszelka krytyke.
Ten post edytował q.michal 14.02.2016, 18:06:41 |
|
|
14.02.2016, 13:13:11
Post
#2
|
|
Grupa: Zarejestrowani Postów: 8 068 Pomógł: 1414 Dołączył: 26.10.2005 Ostrzeżenie: (0%) |
- Konstruktor bez sensu.
- getInstance referencja ble.... - Kod addValue(0, null); isRegistered(0) -> false |
|
|
14.02.2016, 13:36:29
Post
#3
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
- Konstruktor bez sensu. Mozesz nieco rozwinac? - getInstance referencja ble... Masz racje, referencje mozna wywalic. - Kod addValue(0, null); isRegistered(0) -> false Racja, uzyje array_key_exists(); Ten post edytował q.michal 14.02.2016, 13:38:58 |
|
|
14.02.2016, 14:04:07
Post
#4
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
Cytat Cytat(Pyton_000 @ 14.02.2016, 13:13:11 ) * - Konstruktor bez sensu. Mozesz nieco rozwinac? no bo co on daje, nic, równie dobrze mogło by być tak
poza tym to singleton wiec nie robi się jawnego konstruktora |
|
|
14.02.2016, 14:10:44
Post
#5
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
To wszystko prawda, jednak nadal nie rozumiem co jest zlego w tym konstruktorze i dlaczego nie powinno sie go stosowac w przypadku singletonu?
Efekt jest ten sam, kiedy chodzi o dzialanie aplikacji, dlatego licze, ze ktos wyjasni mi ta roznice |
|
|
14.02.2016, 14:17:51
Post
#6
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
bo w singletonie chodzi przecież o to żeby była 1 instancja tego samego obiektu, wiec jak skonstruujesz 2 to już nie będzie to singleton
https://pl.wikisource.org/wiki/Singleton_(w...towy)/kod#PHP_5 |
|
|
14.02.2016, 14:32:35
Post
#7
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
bo w singletonie chodzi przecież o to żeby była 1 instancja tego samego obiektu, wiec jak skonstruujesz 2 to już nie będzie to singleton https://pl.wikisource.org/wiki/Singleton_(w...towy)/kod#PHP_5 Byc moze czegos nie rozumiem, ale w dalszym ciagu jezeli uzyjesz operatora new, zamiast metody getInstance to dostaniesz 2 rozne obiekty, bo to ona dba o to, aby byl to singleton. Poza tym metoda getInstance pozwala na utworzenie wielu rejestrow. Na czym ma wiec polegac rzekoma roznica? |
|
|
14.02.2016, 14:49:14
Post
#8
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
no wiec nie rozumiesz singletona, po to stworzyłeś metodę getInstance(...) , żeby wywołać go tak
Cytat $singleton = Registry::getInstance(); operatora new nie używasz, dlatego konstruktor musi być prywatny, żeby go przez przypadek nie wywołać, a jak spróbujesz to wywali bład.http://ideone.com/hJodgn Skoro tworzysz wiele singletonów to poco Ci pattern singleton, skoro łamiesz jego podstawowa zasadę i traci on wgl sens. Ten post edytował com 14.02.2016, 14:46:24 |
|
|
14.02.2016, 14:55:44
Post
#9
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
Inaczej.
Zgadzam sie, ze nie da sie uzyc operatora new jezeli konstruktor jest prywatny. Niemniej jednak w dalszym ciagu nie widze roznicy pomiedzy inicjalizacja zmiennej podczas jej definicji, a w konstruktorze? Znalazlem natomiast dzieki Tobie blad w kodzie - konstruktor powinien byc prywatny, a u mnie jest publiczny. Ten post edytował q.michal 14.02.2016, 14:57:30 |
|
|
14.02.2016, 14:58:49
Post
#10
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
nie ma różnicy, ale w konstruktorze wypełnia się ją wartościami domyślnymi, poza tym konstruktora nie wywołujesz, wiec to się nigdy tam nie ustawi. (jawnie za pomocą operatora new, zbyt duży skrót myślowy) Poczytaj jak działa singleton, bo kompletnie go nie rozumiesz. I w takiej implementacji jak zrobiłeś jego użycie nie miało sensu.
Ten post edytował com 14.02.2016, 15:19:52 |
|
|
14.02.2016, 15:08:39
Post
#11
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
Jezeli konstruktora nie wywoluje, to:
nie powinno chyba zwrocic ponizszego wyniku? Kod array(0) { } Oczywiscie kontruktor jest prywatny, tak jak w 1 poscie. Chyba, ze naprawde czegos nie rozumiem? |
|
|
14.02.2016, 15:17:07
Post
#12
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
nie wywołujesz go jawnie, wiec ustawianie w nim czegoś jaki ma sens? konstruktor wykorzystuje się do tworzenia obiektu i ty go tworzysz tu:
Wiec on się tam wywoła, tylko ustawianie w nim pustej tablicy na elemencie nie ma sensu, btw wgl brakuje Ci pola $instance. Z singletona zrobiłeś sobie zwykła klasę tylko na około nie wiedzieć poco. bo mogę zrobić tak:
Jeśli masz taki przypadek
Wtedy to ma sens, ale jak masz prywatny konstruktor to nic to nie daje Ten post edytował com 14.02.2016, 15:25:37 |
|
|
14.02.2016, 15:26:50
Post
#13
|
|
Grupa: Zarejestrowani Postów: 361 Pomógł: 22 Dołączył: 10.02.2015 Ostrzeżenie: (0%) |
Jeśli już o singletonie mówiąc to nie powinno się jeszcze dodać prywatnej metody __clone?
-------------------- |
|
|
14.02.2016, 15:32:54
Post
#14
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
Fred1485 owszem
q.michal poczytaj o registry pattern i napisz go jeszcze raz tu masz przykład http://www.phpbar.de/w/Registry |
|
|
14.02.2016, 18:04:30
Post
#15
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
Ale chodzi tylko o ten kontrowersyjny konstruktor? Czy ogolnie uwazasz, ze wzorzec zostal zle zaimplementowany?
|
|
|
14.02.2016, 18:52:47
Post
#16
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
no poco tam wepchałeś singleton, który stracił sens singletona.
Wyrzuć ten konstruktor, ten przykład który Ci dałem miał zobrazować kiedy się go używa a konstruktor robi się prywatny własnie po to żeby go nie używać wiec też go nie używaj. Uparłeś się żeby go mieć i nauczysz się złych praktyk w ten sposób.
Robiąc mały test, dla singletona spodziewasz się że $rega === $regb oraz że $regb->getRegistry() i $rega->getRegistry() zwróci array(1) { [0]=> string(4) "test" }, bo na tym polega antywzorzec singleton, a u Ciebie on nie ma sensu bo to są dwa rożne obiekty, ale zamiast jawnego konstruktora maja metodę Instance tak działa singleton: http://ideone.com/RUd4EU |
|
|
14.02.2016, 19:18:56
Post
#17
|
|
Grupa: Zarejestrowani Postów: 111 Pomógł: 1 Dołączył: 24.12.2013 Ostrzeżenie: (0%) |
Ale rozumiesz, ze getInstance zwraca zawsze ta sama instancje rejestru o tej samej nazwie? Dzieki temu mozesz stworzyc kilka rejestrow. Jezeli nie przekazesz nazwy - uzyje domyslnej.
Jezeli zmodyfikujesz powyzszy kod do:
to to co piszesz bedzie prawda. |
|
|
14.02.2016, 19:28:17
Post
#18
|
|
Grupa: Zarejestrowani Postów: 3 034 Pomógł: 366 Dołączył: 24.05.2012 Ostrzeżenie: (0%) |
Ale na tym polega singleton, jeśli używasz wzorca, to go używaj zgodnie z definicją.
Da Ci dokładnie to samo jak użyjesz depedency injection. Jak chcesz mieć fabrykę to używasz factory pattern a nie robisz z singletona... Ten post edytował com 14.02.2016, 19:26:35 |
|
|
14.02.2016, 19:34:08
Post
#19
|
|
Grupa: Zarejestrowani Postów: 361 Pomógł: 22 Dołączył: 10.02.2015 Ostrzeżenie: (0%) |
Może jakbyś zrobił abstrakcyjną klasę Registry, a potem poszczególne klasy dziedziczące po niej i każda byłaby singletonem, chyba że nazwy mają być zmienne. Tak na moją głowę
-------------------- |
|
|
Wersja Lo-Fi | Aktualny czas: 26.09.2024 - 02:15 |