I think it is easier to get design right with object oriented style. You have high level objects, which operate on lower level objects, which in turn operate on even lower level objects. You can spin this both ways, see?
No, this isn't true. If you have an object, this object handles all operations himself (if you've done OOP "right"). So that would be both lower and higher level operations.
The primary advantages of classes are:
- Modularity (irrelevant since in php you're dealing with individual files...everything is already highly modular)
"Modularity - A quality of a system where it consists of various parts which separate cleanly and fit together well. High modularity costs some design time but pays back well through clarity, elegance, maintainability and flexibility."
I think you have better modularity (on a small scale) if you use functions because every function is essentially a module, "which separates cleanly". With OOP, an entire class is a module, and the functions depend on each other's effects, so a *class* is a module. I don't know if this makes a huge difference on this small level, but it could.
- Encapsulation (depending on context can be important or not. Generally speaking, it's a lot easier to encapsulate data with classes than without, but php's file-scoped nature means that you have other options).
You hide implementation with encapsulation. There is no difference here, because you hide implementation in methods or functions. You still hide the implementation in the same way.
- Reusability (largely irrelevant to a dynamically typed, interpreted language since you can very easily reuse the same function call or include line for many different types of data without issue)
I assume we're talking about intra-application-re-usability, and not inter-application. Practically the only thing you can share between applications is a database class/are database functions.
As you said, you can easily reuse the same function or class. I think that functions are more reusable, because you could be able to reuse one (or two) methods of a class for a new one. You can't do that (unless you inherit from the existing class, which is a problem if you want to inherit from another class too), but with functions, you can easily reuse one function for different things. I don't think there is much difference here if you have multiple inheritance or something like mix-ins, but PHP doesn't have these.
- Aiding in self-documenting code (ppp methods are more about documentation than actually enforcing anything. Many languages don't actually enforce the constraints at run time, and are only checked at compile time).
I don't think that there is a difference here, besides that the name of the class is explicit if you define it. You could easily add a comment like:
Code:
# these functions work on #{kind-of-thing}
But there is a problem here. In OOP, methods belong to an object, but if you use functions, they don't necessarily belong to the first argument of the function.
Example:
Code:
function add_to_todo($todo_list, $item){}
Does this function belong to the todolist or to the item?
And if we'd written it like this:
Code:
function add_to_todo($item, $todo_list){}
(I don't like the order of the arguments in this function personally)
If you use a class, that function would probably be in the TodoList class. If I'm uncertain about the order of arguments I want a function to have, I ask myself in which class I would put it if I were using OOP, and use that as the first argument ;-). So maybe some things are easier to get "right" if you use OOP.
Not really. You are assuming a procedural mind set, which of course will lead to similar code in the to syntaxes.
The syntax really doesn't matter. CLOS (yea, that's a thing in a paren language) uses this syntax:
Code:
(method object arg1 arg2 .. argn)
instead of:
Code:
$object->method($arg1, $arg2, .. $argn)
In the example, you gave (the second), there is a static coupling between the utilizing code and the function Thing(). In the first example (which uses OOP syntax), you could pass the object in, and thus decouple that relationship.
That's because you can't dispatch functions on arguments in PHP. With OOP, you can at least dispatch on one argument (the first). If you have a good type system, you can dispatch on all arguments (which means polymorphism on all arguments). But you're right, this is one advantage of OOP in languages without a good type system.
I agree with some of the things you've said but I also think that OOP is not just for large systems. Any non-trivial piece of code will benefit from OOP. You will find different roles - ie objects - if you hunt them down.
So, what is the benefit, besides having to hunt down the objects?
Bookmarks