Re: Re: OOP Hello World
| From: | Martin Alterisio | Date: | Thu, 05 Oct 2006 13:24:13 +0000 |
| Subject: | Re: Re: OOP Hello World | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-242661@lists.php.net to get a copy of this message | ||
Double-checking.....
Nope.
It wasn´t from the Java mailing list, they're still talking about
hibernate... they'll probably give a damn about this thing anyway, because
they're all oop gurus, what would they care about a hello world?
PHP seems to be getting more and more object oriented, I think it's the
right time to start questioning what have been done so far in terms of OOP
in PHP, because, honestly, there are too many php classes being distributed
out there that are a complete mess. Forget about spaghethi code, we have to
deal with pizza classes too.
2006/10/5, John Wells <wellsdjohn@gmail.com>:
Are you sure you're not on a Spanish *Java* mailing list? :) On 10/5/06, Martin Alterisio <malterisio777@gmail.com> wrote: Me... again, still boring you to death with meaningless OOP rantings. First I would like to point out that there was a design mistake in what I proposed in the last mail (following afterwards): the Saluter shouldn't be an abstract class, it isn't correct to base your class hierarchy on the ability to greet (or in the ability to do something generally speaking).. If it's done that way we'll have classes coexisting in the same branch of the class tree, but possibly having completely different nature. Saluter should be an interface: interface class Saluter {public function greet(Salutable $receiver);} class FormalSaluter implements Saluter {public function greet(Salutable $receiver) { echo "Hello " . $receiver->getSalutationName() . "\n"; }} Then, I'll try to translate what has been said in the spanish list. It has been proposed that World should be a singleton, since there is only one world... but this arguable, at metaphysic level perhaps... Also an observer pattern could be used so anyone can hear the greetings.. But I think this should be done on the design of the greeting enviroment abstraction (to provide other places than standard output where the greeting can be said). The saluters factory should use a factory method pattern or an abstract factory pattern? I think an abstract factory is not appropiate in this case, the creation process is not that complex to require this kind of design pattern. Also I proposed that decorators could be used to add styles to the greeting for outputs that allow styling (e.g.:html). In this case maybe an abstract factory would be appropiate, so that decorated saluters are created for the appropiate type of output, combined with a builder pattern to create the decorated saluters (this last part I thought it just now, I'll post it later on the spanish list) Well, I think that sums it all up. 2006/9/29, Martin Alterisio <malterisio777@gmail.com>:What's up folks? I just wanted to tell you about a thread that's going on in thespanishphp mailing list. A fellow software developer which had just startedwithOOP was asking for "Hello World" examples using OOP. The examples ofcode hehad been doing was not that different from the usual Hello Worldexample weall know and love(?), so I thought he was missing the point of whatwas thepurpose of using OOP. This was my reply (I'll try to keep thetranslation asaccurate as possible): I believe you have misunderstood what is the purpose of OOP, yourobjectsare not proper abstractions. First and most important, you should define what is the problem tosolve:greet the world. We build an abstraction of the problem and its main components:somebodywho greets and something to greet. Here we're working with generalizations, since our main objective istobuild reusable objects. For that purpose is useful that our "saluter"couldbe used to greet more than just the world. Therefore we'll first define what kind of interaction we expect the salutation receivers to have, in the following interface: interface Salutable {thepublic function getSalutationName();} In this interface we have all we need to properly greet any entity:name we should use when doing the salutation. Then we create the object which represents the world: class World implements Salutable {leavepublic function getSalutationName() { return "World"; }} Now we're missing a saluter, but we're not sure which way of greeting would be appropiate, so we prefer to create an abstract saluter andthe child implementation to decide the appropiate greeting: abstract class Saluter {worldabstract public function greet(Salutable $receiver);} In our case we need a formal saluter as we should not disrespect the(saying "hey! wazzup world?" could be offensive), then: class FormalSaluter extends Saluter {Maybe itpublic function greet(Salutable $receiver) { echo "Hello " . $receiver->getSalutationName() . "\n"; }} Finally we make our saluter greet the world: $saluter = new FormalSaluter(); $world = new World(); $saluter->greet($world); ---- Other things you should keep in mind: * PHP's type hinting is preety limited, in this case we would like to indicate that the name should be provided as a string but we can't.would be useful to use an object as a wrapper for native strings.EDIT: Iremembered while translating this that type hinting can only be usedinfunction parameters, therefore this point is useless. * En this model it seems more appropiate that the saluter is anabstractclass, since salutation works one way, but, in the event salutationsbecamea two way trip, an interface would be more appropiate for thesaluters.* Here we're sending the salutation to the standard output, which is acceptable in this case, but a more complex abstration would requireus toindicate where we should procede with the salutation, and we will havetoprovide an abstraction for the possible salutation enviroments. * A factory of saluters would be a nice feature. ------------ That's it. If you find this interesting I'll try to keep up with the translation of following posts.