oop question

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
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: oop question

Post by Jenk »

And my answer to that is two fold:

1. I already work in a team, and we write code that is used by other teams as well.

2. No, they are are not needed. Documentation (comment or other) will provide enough of a description to warn others of misuse. Tests will identify failures. Developer intelligence will do the rest.

If they were so necessary, how on earth did PHP, C, Smalltalk, etc. survive?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: oop question

Post by onion2k »

Jenk wrote:If they were so necessary, how on earth did PHP, C, Smalltalk, etc. survive?
They're not necessary to have in the language per se because you can code implement the same thing in documentation/communication, but they make for better code if they're available. Having the language enforce the rules rather than trusting the other developers is infinitely preferable. I consider that to be the same as needing to use them if they're there.

I've lost count of the number of times I've seen scripts fail "randomly" because of scoping issues, especially with things in the global scope (hence the reason I'm always posting here that people shouldn't use the "global" directive in PHP). If your code can defend against that sort of problem with pretty much no penalty then why wouldn't you do it? What benefit is there in not using private, protected and public?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: oop question

Post by Jenk »

Answer: So you have better flexibility with your objects and classes.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: oop question

Post by onion2k »

Jenk wrote:Answer: So you have better flexibility with your objects and classes.
If you want the flexibility of being able to, for example, set a class variable from outside of the class then the directives let you - you define it as "public". If you don't want people to then you set it as "private".

Setting everything as public, which is what you're suggesting if you don't use them, is less flexible.

So, again, what is the benefit of not using the directives? Why would you set everything as public even if you want the variables or methods to be private?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: oop question

Post by Jenk »

How on earth is it less flexible to open up to more possibilities? That is the very definition of "flexible".

I might want them to be 'private' for now, in this context, on this project, at this current time. 6 months later, on a different project, or even the same project with a new requirement I might not.

A script failed because someone (to continue the example from earlier) set num to -10. Whoopdedoo. Tests will identify this, which will be run anyway, the comment will say "Should be >0" and immediately lets the developer know.

In fact, this may be better than setting to -10 and your code abs()ing it, the developer have a puzzled look on his face and not immediately seeing why his script is failing..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: oop question

Post by onion2k »

Your point about "flexibility" is silly. You might as well advocate putting everything in the global scope because "it'd be more flexible" to be able to access every variable in every class and function. It'd be true, but it'd be appallingly bad code. You should limit things to being accessed where they're needed and nowhere else. That means they should be private unless they're needed in classes that extend the base class whereas they'll be protected, and only public if there's a definite need for them to be accessed from outside of the class - that will be very rare. If your code does that then you should be asking serious questions about it's functionality.

I'd love to be there when you encounter PHP6's namespaces.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: oop question

Post by jazz090 »

but sometimes these scope identifiers can be real pain in the ass as i was working with the zend framework and tried to mess around with it a little bit and the god damnd protcted value didnt let me. however this raises another question: if you are working as a team and you are distributing the code (Zend, phpBB,...) cant the back end users just change the code to make a protected public? i would assume it would be more like voiding the waranty on your pc by opening the case (its guarnteed to work [for some time] unless you void the warantee)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: oop question

Post by Jenk »

onion2k wrote:Your point about "flexibility" is silly. You might as well advocate putting everything in the global scope because "it'd be more flexible" to be able to access every variable in every class and function. It'd be true, but it'd be appallingly bad code.
You've gone from borderline paranoid right into paranoid now.
You should limit things to being accessed where they're needed and nowhere else. That means they should be private unless they're needed in classes that extend the base class whereas they'll be protected, and only public if there's a definite need for them to be accessed from outside of the class - that will be very rare. If your code does that then you should be asking serious questions about it's functionality.
I'm asking serious questions about your mentality and trust of other developers [EDIT: To be clear, anyone with the mentality of "Restrict as much as possible"]. Who are you to question my/their intentions with your code?! Can you really be so arrogant to think you are certain they won't need access to them? Simple answer for you: No, you absolutely cannot. You can comment on the member, its function, its purpose, its constraints, but to completely restrict it is nothing short of paranoid.
I'd love to be there when you encounter PHP6's namespaces.
Already got beef with namespaces, and have stated so many times, and a separate beef with PHP's implementation of "We'll do them just because Java does."
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: oop question

Post by onion2k »

Jenk wrote:
onion2k wrote:Your point about "flexibility" is silly. You might as well advocate putting everything in the global scope because "it'd be more flexible" to be able to access every variable in every class and function. It'd be true, but it'd be appallingly bad code.
You've gone from borderline paranoid right into paranoid now.
You might think of it as paranoid, I think of it as careful. I don't want my team to use code I've written in ways that they're not meant to. I can trust them not to, or I can write proper interfaces that stop them.
Jenk wrote:
You should limit things to being accessed where they're needed and nowhere else. That means they should be private unless they're needed in classes that extend the base class whereas they'll be protected, and only public if there's a definite need for them to be accessed from outside of the class - that will be very rare. If your code does that then you should be asking serious questions about it's functionality.
I'm asking serious questions about your mentality and trust of other developers. Who are you to question my/their intentions with your code?! Can you really be so arrogant to think you are certain they won't need access to them? Simple answer for you: No, you absolutely cannot. You can comment on the member, it's function, it's purpose, it's constraints, but to completely restrict it is nothing short of paranoid.
Yes, I can be that arrogant. I design software with future extensibility in mind, and if there's no reason for something to be public then I made it private. If I don't then someone can use it in a way I don't want them to, and then it's up to me to explain why their code is failing in my library. That's a waste of my time.

If you want a good example try looking at a library like SwiftMailer. Most of the methods are private. If they weren't then Chris would spend lots of time answering questions about why someone's code doesn't work when they access something directly instead of using the right accessor method. That'd be a waste of time.

Your fear of "what if it needs to change later?!" is making you compromise your code now. You should write code how it should be written now and worry about changing it later if it needs to change.
Jenk wrote:
I'd love to be there when you encounter PHP6's namespaces.
Already got beef with namespaces, and have stated so many times, and a separate beef with PHP's implementation of "We'll do them just because Java does."
It's not "just because Java does". It's a system of ensuring things are neatly encapsulated and don't clash. It's a good thing. Although PHP6's implementation isn't as tidy as it could be admittedly. Still better than nothing though.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: oop question

Post by Eran »

Protected / private properties are similar to variables declared within a function. They are meant for inner class use and can easily change between small revisions of the class as refactoring takes place. Changing the public API and properties should be minimized as external classes probably depend on it and those changes propagate through the system. By making clear what properties / methods are meant for internal use, you reduce overhead later when changes to the structure of the class are made. If you can accept the scoping benefits of functions, class based scoping shouldn't be much of a stretch.

Affect inner class properties through public methods. Interacting with the properties directly couples your code with the internal working of the class and is bound to create issues when the class changes and evolves.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: oop question

Post by Christopher »

Jenk wrote:To answer the private/protected: A decent comment/documentation does this much better, because it doesn't have the insult (loose definition of the word) of restricting it when/if the need to access it becomes necessary.
Jenk wrote:I disagree. You don't need to have "private" just to be able to code confidently without side-effects. Just because it's "public" doesn't mean it'll instantly get used. A comment describing the member variable/method and its purpose will suffice. Honestly, what you've described is borderline paranoid programming.
I read through the posts and I have to say I am baffled. If your attitude was that you understood about limiting scope but just don't want to be bothered, I could understand. But you actually say that limiting scope is insulting to other programmers and borderline paranoid. By this logic using local variables or constants is also insulting and paranoid, because some programmer later might want to set them. I really don't get your idea that a comment is the same as something that gives a compiler error?
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: oop question

Post by Benjamin »

I don't think that words such as insulting and borderline paranoid apply to programming methodologies. Relax. Personal attacks immediately disqualify your argument.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: oop question

Post by Jenk »

I use those words because they (by their dictionary definition) provide the best description. I don't do sugar coating, I just want to say what I mean, likewise in return is appreciated. :)

"You should write code how it should be written now and worry about changing it later if it needs to change."

That's ironic. You're restricting code because you don't want others to access it. I'm just not restricting it. Which, of those two, is apprehensive of future use?

It's not up to you to explain why their code is failing, if they are improperly using your library. It's up to them to use your library properly.

FWIW I'm not advocating the direct interaction of member variables, infact I don't think I ever access them outside of testing (and even then it's only when testing wrappers or such and I need to place a mock or stub.) but I just don't see any restriction necessary. Ever.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: oop question

Post by Christopher »

Jenk wrote:I use those words because they (by their dictionary definition) provide the best description. I don't do sugar coating, I just want to say what I mean, likewise in return is appreciated. :)
:roll:

So let me get this straight, you are saying:
Jenk wrote:That's ironic. You're restricting code because you don't want others to access it. I'm just not restricting it. Which, of those two, is apprehensive of future use?
You think that if you do not want others to access something-- then you should not restrict access it.
Jenk wrote:It's not up to you to explain why their code is failing, if they are improperly using your library. It's up to them to use your library properly.
Who cares if they are accessing something they shouldn't. Your not going to help -- they should just know they shouldn't (or have read all the comments in the source).
Jenk wrote:FWIW I'm not advocating the direct interaction of member variables, infact I don't think I ever access them outside of testing (and even then it's only when testing wrappers or such and I need to place a mock or stub.) but I just don't see any restriction necessary. Ever.
You don't even think that properties should be accessed directly and I assume that you design your code with that assumption. But even though you think all properties should be used as if they were at least protected, you make them public anyway so in the future people can use them even though they are not intended to be used that way.

:banghead:
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: oop question

Post by Jenk »

No, I do not want to restrict others from accessing it. That's the point I've been making *all* along. The comments will describe the purpose of the member, it's constraints and functionality. That's it. It's the developer's judegment to use it or not. I'm not going to make his or her call for him by restricting access.

And since when has this discussion been restricted [sic] to variables only?

Maybe you should take astion's advice and relax, if you're banging your head against the wall..
Post Reply