Re: OOP versus Procedural/Functional [message #177581 is a reply to message #177556] |
Tue, 10 April 2012 12:49 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma:
|
Senior Member |
|
|
.oO(Mr. B-o-B)
> I was curious about peoples thoughts in regards to OOP with PHP.
>
> Are there pro's/con's of writing OOP code versus procedural/functional
> coding?
>
> Strictly speaking for PHP, I do the bulk of my coding as
> procedural/functional. I really haven't found any added benefit to going
> the OOP route.
>
> Am I missing something, or is it just a different approach to the same
> problem?
Besides what the others have written, there are some things that I
really like about using OOP in PHP:
* comfort
With PHPDoc comments and a good IDE (e.g. Eclipse/PDT) you can get many
assistance when writing code. You type an object variable, and since the
IDE knows from the comments of what class it is, it automatically
suggests available method names with their descriptions, parameter lists
etc.
* security
You can define access restrictions on methods and member variables and
type hinting on parameter passing, so that PHP can check if a passed
object belongs to a particular class or interface.
And most important for me:
* inheritance/polymorphism
You can easily extend the functionality of a class by deriving a child
class from it. But you can also still use the child class in a context
where an object of the base class is required, without having to change
any code.
I use this in my form classes for example: For all the basic form
controls (input fields, select boxes, password fields, ...) I have my
classes. If I should now need a more special control, e.g. a select box
with some predefined values, I simply derive a child class, add some
other initialization code and that's it. Just a few lines. In every
other context, e.g. when creating the form or processing its data, I
don't have to change anything, I can use the new form control like any
others.
Another example: When I call the output methods of my visual objects
(all the stuff, that represents something on an HTML page), I just have
to make sure the objects are derived from the base class for these
visual objects, because then I know that all derived classes will also
have a render method and be able to output something. I don't have to
know what these objects are and what they represent, I just tell them
"render yourself" and that's it.
Micha
--
http://mfesser.de/blickwinkel
|
|
|