Title: Using headers in a class
Tags: php
Question: I access a function in an included class Environment which can throw an Alert which needs to direct the users back to a page with some error information. What is the best way for me to reference the page so it can be used wherever I access the function from anywhere?
Is there any better way to do what I am trying to do?
public function checkEnvironment() {
try {
// If status is false
if (!$this-&gt;getStatus()) {
// Generate a new special exception with code
throw new Alert(6);
} else {
$connection = Gateway75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0heckInstance();
return $connection-&gt;getData('SELECT * FROM control_environment WHERE subdomain = ?', array($this-&gt;subdomain[0]));
}
} catch (Alert $alert) {
$_SESSION['error'] = $alert-&gt;getData();
if (!headers_sent()) {
header('Location: my/file/here.php');
exit;
}
}
Comment: Now updated, still doesn't answer my question though

Comment: somewhat pointless. it could only ever work if no other output has yet been performed elswhere in your script. you should be at least checking `headers_sent()` and decide from that if redirect would cooleyelizabeth@example.org.

Comment: `// If status is false` is also a particularly bad comment. You should only add comments to lines that require clarification, they shouldn't be an English translation of lines which are already clear.


Here is another answer: You're badly abusing exceptions. There is absolutely no reason to put a single if statement in a try block, which can only throw one type of exception which is guaranteed to be caught immediately after that block. You've added nothing to your code but clutter, there is literally no advantage to doing this over simply doing an if/else with  no exceptions/catching.
The point of exceptions is that you throw them up out of the current scope, to some place where they can actually be handled in a meaningful way.
Pick a real exception class, one that communicates something about the error that's occurred. Alert(6) doesn't tell anybody anything. Then, handle that (and probably many other) exceptions up above this, where you can be more sure that redirecting is the correct course of action. Your low-level database code shouldn't have any concept of browsers or http or redirection.
Your code also shouldn't have an else branch when the purpose of the if branch is to throw an exception. The else is redundant.
The whole function should look like this.
public function checkEnvironment() {
if (!$this-&gt;getStatus())
// Generate a new special exception with code
throw new StatusException;
$connection = Gateway75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0heckInstance();
return $connection-&gt;getData('SELECT * FROM control_environment WHERE subdomain = ?', array($this-&gt;subdomain[0]));
Comment for this answer: So if I throw it here then on the page it was called catch it, that will work?
Comment for this answer: Yes, so long as that page contains a `catch` block.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:02/14/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php>', 'Title': 'Using headers in a class', 'ViewCount': 48, 'id': 14881594, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}