OOP Hello World

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



What's up folks?

I just wanted to tell you about a thread that's going on in the spanish php
mailing list. A fellow software developer which had just started with OOP
was asking for "Hello World" examples using OOP. The examples of code he had
been doing was not that different from the usual Hello World example we all
know and love(?), so I thought he was missing the point of what was the
purpose of using OOP. This was my reply (I'll try to keep the translation as
accurate as possible):

I believe you have misunderstood what is the purpose of OOP, your objects
are not proper abstractions.

First and most important, you should define what is the problem to solve:
greet the world.
We build an abstraction of the problem and its main components: somebody who
greets and something to greet.
Here we're working with generalizations, since our main objective is to
build reusable objects. For that purpose is useful that our "saluter" could
be 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 {
   public function getSalutationName();
 }

In this interface we have all we need to properly greet any entity: the name
we should use when doing the salutation.

Then we create the object which represents the world:

 class World implements Salutable {
   public 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 and leave the
child implementation to decide the appropiate greeting:

 abstract class Saluter {
   abstract public function greet(Salutable $receiver);
 }

In our case we need a formal saluter as we should not disrespect the world
(saying "hey! wazzup world?" could be offensive), then:

 class FormalSaluter extends Saluter {
   public 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. Maybe it
would be useful to use an object as a wrapper for native strings. EDIT: I
remembered while translating this that type hinting can only be used in
function parameters, therefore this point is useless.

* En this model it seems more appropiate that the saluter is an abstract
class, since salutation works one way, but, in the event salutations became
a two way trip, an interface would be more appropiate for the saluters.

* Here we're sending the salutation to the standard output, which is
acceptable in this case, but a more complex abstration would require us to
indicate where we should procede with the salutation, and we will have to
provide 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.

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux