Coding Style: passing values to functions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

ik wrote: Yes, I agree. Unforunately PHP classes don't have private fields/methods, so user is allowed to directly rewrite any field anyway. And in my case user is the same person as class writer :D
Well PHP5 does now... And even if it didn't you can still restrict yourself from using the members directly to keep abstraction/encapsulation. Some people use a naming convention such a prefixing an underscore to "private" attributes to remind themselves not to use them directly, etc. And even if the same person is the class author and class user, you'll thank yourselve 6 months from now when you have to modify the class and don't have to touch the code that uses the class.....


Going off-topic....

Two common OOP fallacies:
1. Every attribute must have its own get/set
2. Every non get/set method takes no arguments (Edit typo correction)
Well, I am not pretty sure about both :D
Especially about 2. Foo->Run() is a quite common construct in any language.[/quote]
I'm not saying its wrong to have function like foo->run(). I'm saying its wrong to force every non get/set function into that format, as a lot of example code on these forums seem to do. The Orignal Poster seemd to imply that the only way to pass arguments in "proper" OOP fashion was to use the set's before calling the real function.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

I think their are a lot of 'proper' ways. It all depends on who you ask. =)

It reminds me of a saying: "The nice thing about standards is that their are so many to choose from."
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

True, but when i see someone equating multiple-sets-and-argumentless-function-call with a way of passing parameters I worry about the message we send to people about OOP. That's not "The One True Way" (there isn't one true way) and it looks cumbersome, inelegant which is likely to turn someone from learning more about OOP. I've seen people replace five related functions, with five classes, doing exactly the above. When it would seem appropriate to use a single class with five methods.... (all took the same paremeters, 3-5 functions were used per script)

So where the orginal code would have been five lines of function calls, the new code was five blocks of new Foo, foo->sets, foo->do. This is definately not what OOP is about. I would rather have seen a single class, a single call to the constructor to set up the variables and five argumentless calls. That seems to me to show the beauty and power of OOP. If you need to change a parameter between some method invocations, sure use the set method, but don't overload it for argument passing as a routine.
Post Reply