Re: when is OOP a good choice?
| From: | SLanger at spirit21 dot de | Date: | Thu, 13 Feb 2003 08:41:27 +0000 |
| Subject: | Re: when is OOP a good choice? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-135396@lists.php.net to get a copy of this message | ||
Hello
There isn't an easy answer to this but I guess the most important thing
being that it is easier to maintain in big projects and this makes it more
robust since you can test in a more modular way. Also it makes it easier
to reuse code also this seems to be one of those urban legends. At least
most people I have seen so far tend to throw old code away and reimplement
the wheel... well its getting better.
Especially good Frameworkdesign is a lot easier with OO.
There is one drawback to OOP using PHP at least in version 4. (This is
supposed to change in 5)
PHP doesn't support the concept of abstract classes (can be emulated) and
function overloading (two functions having the same name but different
signature). This makes it hard to implement polymorphism in some
situations (IMHO) which is one of the key concepts of OOP.
If you are coming from a different OO Language like JAVA it tends to screw
you up that PHP Objects only live for the duration of the script. So you
have to be sure to recreate objects on every request or you have to use
sessions in order to preserve Objects. This makes it a bit of a pain IMHO
to use OO in PHP.
As I see it (correct me if I'm wrong) in version 4, OO is just a way to
construct modular PHP that can be easily extended for custom behaviour.
So to your second point I'd say:
If you have a very complicated and long script, which can be broken down
into modules, so that each module can function on its own then it would be
a good choice to create this module as a class. Further if you plan on
extending the behaviour of this module (add new functionality or override
some functionality) thenyou have to create it as a class since only OO
allows you to override the functionality.
Another pro for OO is when you have modules (objecty/classes) that you are
reusing very often, but these modules change in functionality it is also a
good idea to create classes. (Example: DB access, each DB handles access
differently. So create a base class with a common API for DB access and
extend the actual DB handling classes from that. When your scirpt uses the
base class to interface with the DB you can easily switch databases by
simply switching the implementing class. NOT sure this makes sense ; ))
On the other hand if you simply need to do something over and over, e.g.
change characters into entitites and viceversa, a function is better
suited.
Basically it is a design choice and there is no real pro and contra for
the one or the other. There are enough people out there that do OO but
don't really use OO.
Hope that shed some light on the matter
Stefan