2

I'm working with classes in PHP. When I'm writing a class, I'm always thinking "This object is basically a one-off; it's not going to last beyond the page load." Consequently, all the logic in my classes basically construct themselves, do a few state changes, give some feedback, and die. Brine shrimp.

Because of this, I've taken to using exceptions to throw almost any and all problems, because I won't be able to have the user interact with the object in case of a problem. Instead, the user gets some feedback on the page about what happened, and then they re-submit the form or whatever. The object is about to die in a few milliseconds anyway, and I won't be able get more input to handle the error.

It seems to me that if I were programming in an environment with persistent objects, I would probably make more robust warning and error reporting and handling in objects, before I had their method terminate with an exception. If an object was still hanging around with an error or warning state, I could get some more input from the user and proceed. So my objects would look and behave differently.

Please note that what I'm asking is not "How do objects behave differently in PHP from other languages?" but rather "When writing object in PHP (or other non-persistent environments), how do those objects differ from when they are able to persist?"

user151841
  • 17,377
  • 29
  • 109
  • 171
  • Interesting question. (I wonder what the ramifications are beyond disposing of resources cleanly to aid garbage collection, etc.) I'd be tempted to community wiki this. – John Parker Aug 12 '10 at 14:08
  • You can make object persist in PHP as well, I would just remove any mention to PHP, it's just irrelevant to the issue at hand – Vinko Vrsalovic Aug 12 '10 at 14:38
  • @Vinko I think it's common to use non-persisting objects in PHP... what other environments have non-persisting objects anyway? – user151841 Aug 12 '10 at 15:12
  • @user151841 All environments have non persisting objects, it's usually the programmer's choice whether to persist (and in what way) the objects. It's just that web applications (written in whatever language) by nature of the HTTP protocol that lend themselves to have non persistent objects. – Vinko Vrsalovic Aug 12 '10 at 21:59
  • @Vinko I don't have experience with other web-oriented lanaguges. Is true for say, .NET or RoR, that objects don't persist over multiple requests? If so, I'll remove PHP references. – user151841 Aug 13 '10 at 14:45
  • 1
    You can choose whether to have them persist or not. – Vinko Vrsalovic Aug 13 '10 at 20:18

1 Answers1

0

I suspect one key aspect would relate to the management of resources, with file handles and memory being obvious suspects. (In terms of PHP, I guess you'd unset large arrays and objects that are in-scope yet no longer required.)

That said, I think a good programmer would code in this manner anyway - leaving file handles open, etc. is fairly shoddy practice even if you know the runtime will take care of such things for you.

John Parker
  • 54,048
  • 11
  • 129
  • 129