/    Sign up×
Community /Pin to ProfileBookmark

Exit class, don’t kill the page

I was wondering if there is a way to exit a class object without killing the rest of the page.

the exit and die commands kills everything.

One could use return in the methods, but this wont work since some of the methods are called within other methods, and I want a more automatic way of exiting the class without having to make tons of return value checks in every single method.

So what I need is some kind of exit_class command?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiFeb 27.2010 — You can't exit a class as such, because a class (or more correctly an object) isn't executed procedurally like a method, it is just a data structure. You can exit class [I]methods[/I] by returning, but if that isn't suitable you probably need to rethink how your class is working. From client code you can also unset() the class instance, but obviously this makes no sense from inside the instance itself (though I seem to remember you could unset($this) in PHP4!)
Copy linkTweet thisAlerts:
@NogDogFeb 27.2010 — While it's unclear to me exactly what you want to do, perhaps throwing an exception could be what you need?
One could use return in the methods, but this wont work since some of the methods are called within other methods, and I want a more automatic way of exiting the class without having to make tons of return value checks in every single method.[/quote]
Though one could argue that every call to any method should be checking the return value, and every method should return something. (I try to always follow this rule, though admittedly sometimes I get lazy with regard to checking the result.)
Copy linkTweet thisAlerts:
@dk_zero-coolauthorFeb 27.2010 — Though one could argue that every call to any method should be checking the return value, and every method should return something.[/QUOTE]

True, but that isn't possible in all cases.

By thx both of you.

I didn't expect that an option like this would exist. Just wanted to make sure before starting with some kind of workaround. It would have been easier ?
Copy linkTweet thisAlerts:
@MindzaiFeb 27.2010 — Yes sounds like throwing an Exception and using a try/catch block might be your best bet. Personally I don't like to make my methods all return something just for the sake of it - I prefer to use exceptions and keep the return value free of pollution, especially in the case of public methods where it is really the job of the client code to be looking for exceptions and dealing with them.
Copy linkTweet thisAlerts:
@NogDogFeb 27.2010 — Yes sounds like throwing an Exception and using a try/catch block might be your best bet. Personally I don't like to make my methods all return something just for the sake of it - I prefer to use exceptions and keep the return value free of pollution, especially in the case of public methods where it is really the job of the client code to be looking for exceptions and dealing with them.[/QUOTE]

There are always gray areas and [non-programmatic] exceptions to every rule, but I try to have every method (except constructors and such that cannot) return something, even if it's just boolean true/false to indicate its completion status. I'm of the school that exceptions are for programmer or system errors, but not for expected results -- which include things like user errors, no matching record in the DB, etc.

So for my personal programming style, if the case in question here deals with some sort situation that I would consider an exception, then throwing an exception would make sense. If not, I would have the method in question return false (or whatever makes sense in its case, such as 0 or an empty array/string) and leave it to the calling code to decide what to do.

But I'm not going to claim it's the only way or the best way, just my way. ?
Copy linkTweet thisAlerts:
@NogDogFeb 27.2010 — True, but that isn't possible in all cases.

...[/QUOTE]


Hmm...I'd be curious to know what those cases are. (Not wanting to type some more code does not count. ? :p )
Copy linkTweet thisAlerts:
@MindzaiFeb 27.2010 — There are always gray areas and [non-programmatic] exceptions to every rule, but I try to have every method (except constructors and such that cannot) return something, even if it's just boolean true/false to indicate its completion status. I'm of the school that exceptions are for programmer or system errors, but not for expected results -- which include things like user errors, no matching record in the DB, etc.

So for my personal programming style, if the case in question here deals with some sort situation that I would consider an exception, then throwing an exception would make sense. If not, I would have the method in question return false (or whatever makes sense in its case, such as 0 or an empty array/string) and leave it to the calling code to decide what to do.

But I'm not going to claim it's the only way or the best way, just my way. ?[/QUOTE]


I agree with what you say. What I mean by polluting the return value is returning false for example when an exceptional error occurs. For example a method to get rows from a db table might return false if no (more) rows could be retrieved due to reaching the end of the result set, or having an empty result set, then I would not suggest an exception is the right move. If the method found it had no valid result set though, I would throw an exception rather than return false - if I had a void function I would not return false just to alert the client code to an error. Though I suppose your point is that void functions are not helpful in the first place!
Copy linkTweet thisAlerts:
@NogDogFeb 27.2010 — ...Though I suppose your point is that void functions are not helpful in the first place![/QUOTE]

Yes, I try to have every function at a minimum return a boolean. Maybe I don't care right now whether it returned anything, but I think it makes it more re-usable for future enhancements or porting to other applications.

(Hope we're not veering too far off-topic, DK, but I enjoy these discussions more than the "why am I getting an unexpected $end error?". ? )
Copy linkTweet thisAlerts:
@dk_zero-coolauthorFeb 27.2010 — Hmm...I'd be curious to know what those cases are. (Not wanting to type some more code does not count.[/QUOTE]

Ok, Perhaps there are no such cases, only some that demands more typing ?

But thx, trying to come up with a special case for this reply, just gave me an idea that might work.

(Hope we're not veering too far off-topic, DK, but I enjoy these discussions more than the "why am I getting an unexpected $end error?". ) [/QUOTE]

No that's fine. Discussions is one of the things that make sites like this worth visiting ?
Copy linkTweet thisAlerts:
@NogDogFeb 27.2010 — ...

But thx, trying to come up with a special case for this reply, just gave me an idea that might work.

...[/QUOTE]


Sometimes that's the best result of this sort of discussion, it makes you define the problem more carefully, and then that helps you see a better solution. ? Let us know if you come up with something good.
Copy linkTweet thisAlerts:
@dk_zero-coolauthorFeb 27.2010 — Well I will just create a variable in the class containing a boolean true or false value. If one method creates an error, it can set the variable to true and then I just make the auto methods check that value before each method they execute.

This will give me the result I was looking for. All methods will stop, the class object will still be accessible and the rest of the code outside the object will continue executing.

It's strange that such simple solutions needs this much thinking :S
×

Success!

Help @dk_zero-cool spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...