PDA

View Full Version : [PHP] dubbi Design Pattern


CozzaAmara
18-06-2009, 13:48
Ciao a tutti,

vorrei chiedervi delucidazioni su come sia meglio impostare del codice PHP 5 OOP.

Nello specifico: mettiamo di avere un classico sito di e-commerce nel quale avrò diversi utenti con i rispettivi ordini.

Partendo da un classe "Utente" qual'è il modo migliore per accedere agli ordini dell'utente "X"?

E' corretto creare un metodo, nella classe "Utente", chiamato "getOrdini" che attiva una query sql per recupera gli ordini relativi (come faccio ora), oppure è più corretto creare una classe "Ordine" a cui accedere indirettamente da "Utente"?

Es:
$ordini = $utente->ordini(); // istanzia un oggetto "Ordini"
$lista_ordini_utente = $ordini->getOrdini(); // recupera gli ordini dell'utent X

Con la prima chiamata
$ordini = $utente->ordini(); // istanzia un oggetto "Ordini"

la mia idea era quella di passare l'ID dell'utente al costruttore di "Ordini".

Es:


// sono nella classe Utente
public function ordini() {
$ordini = new Ordine($this->id);
return $ordini;
}


Spero di non aver detto delle cavolate ma con il Design Pattern sono abbastanza a digiuno.

Grazie in anticipo.

CozzaAmara
19-06-2009, 09:38
Up.

annyphp
07-01-2011, 05:58
hi,all.
I was wondering how we should consider the design patterns. I mean: What are they? Are they simply binaries that help us designing consistent application? Are they so used? Why? Where could i find a list about every design patterns on the web? Or at least the 5-10 mist used design patterns? Do phpbb,wordpress or joomla (that i know pretty well) use a design pattern? If yes, then which? I googled MCV PHP and found about 3 italian articles and read about 5 of the first pages ranked and i didn't understand pretty much nothing. They were conflicting and seemed to me confusing. I did understand that it is ...
Fortunatly , I found some popular php design patterns (http://www.phpkode.com/scripts/category/php-design-patterns/) ,which includes decorator pattern, strategy pattern,Singleton pattern,factory pattern listed on the PHPKode.com recently.
I decide to list the result as the topic for all you guys and girls.Hopely can help you !
Best regards!

Shinnok.Exor
07-01-2011, 12:05
Ovviamente se lavori ad oggetti, Ordine è un'entità che ha propri attributi e metodi quindi sicuramente è un oggetto.

Visto che Utente può avere + ordini, io avrei costruito una classe Ordine con tutti gli attributi di un singolo ordine, quali possono essere ID, data, data spedizione eccc....

e poi nella classe Utente avrei messo un array di Ordine.
Nel costruttore l'avrei dichiarato null senza inizializzarlo riprendendo le info dal db (sarebbe inutile, un vero spreco di risorse e di millisecondi (che sul web sono preziosi :) ) inizializzare l'array ordini perchè cmq un utente può anche non visualizzare gli ordini)

poi sempre in Utente avrei fatto un bel metodo Ordine getOrdini ($id = null) che ritorna l'array di Ordine dell'utente inizializzando l'array come attributo.

Kralizek
07-01-2011, 14:25
hi,all.
I was wondering how we should consider the design patterns. I mean: What are they? Are they simply binaries that help us designing consistent application? Are they so used? Why? Where could i find a list about every design patterns on the web? Or at least the 5-10 mist used design patterns? Do phpbb,wordpress or joomla (that i know pretty well) use a design pattern? If yes, then which? I googled MCV PHP and found about 3 italian articles and read about 5 of the first pages ranked and i didn't understand pretty much nothing. They were conflicting and seemed to me confusing. I did understand that it is ...
Fortunatly , I found some popular php design patterns (http://www.phpkode.com/scripts/category/php-design-patterns/) ,which includes decorator pattern, strategy pattern,Singleton pattern,factory pattern listed on the PHPKode.com recently.
I decide to list the result as the topic for all you guys and girls.Hopely can help you !
Best regards!

Hi Anne,
Yours are really interesting questions and the link you shared seems very interesting as well. Unlucky this is an Italian-based board and, as I'm sure you already know, there are tons of forums out there where English is the official language and your suggestions can be fully appreciated.

Anyway, if you have any specific question, please ask the moderators or the management of the board about the possibility to open an English based topic.

Thanks.

Kralizek