One class setting calling another's method
Posted: Mon Jul 12, 2010 8:09 pm
I have a Session class with a method called message. $session->message() stores a message in $_SESSION so that I can send a message from a form processing page back to the form page if some form data is missing or incorrect (i.e. if somebody skips a required field they'll be returned to the form with a message to fill in that field).
My form processing script is in my Customer class. It checks through the form data and if it catches an error it notes it in a field_errors array. At the end, if the array is empty, it returns true. If not, it redirects the user back to the form page. At least it's supposed to, but what's happening instead is I'm getting this message from PHP:
"Notice: Undefined variable: session in /Applications/MAMP/htdocs/rip/includes/customer.php on line 63
Fatal error: Call to a member function message() on a non-object in /Applications/MAMP/htdocs/rip/includes/customer.php on line 63"
The Session class is included before the Customer class, so I don't understand why it thinks session is undefined. Any idea how I can fix this? The relevant parts of the scripts are below.
Thanks!
--------------CUSTOMER CLASS----------------
--------------SESSION CLASS----------------
?>
My form processing script is in my Customer class. It checks through the form data and if it catches an error it notes it in a field_errors array. At the end, if the array is empty, it returns true. If not, it redirects the user back to the form page. At least it's supposed to, but what's happening instead is I'm getting this message from PHP:
"Notice: Undefined variable: session in /Applications/MAMP/htdocs/rip/includes/customer.php on line 63
Fatal error: Call to a member function message() on a non-object in /Applications/MAMP/htdocs/rip/includes/customer.php on line 63"
The Session class is included before the Customer class, so I don't understand why it thinks session is undefined. Any idea how I can fix this? The relevant parts of the scripts are below.
Thanks!
--------------CUSTOMER CLASS----------------
Code: Select all
<?php
class Customer {
public $name;
public $addr1;
public $city;
public $state;
public $zip;
public $email;
public $email_comp;
public $field_errors = array();
public function create() {
if(!$this->name = trim($_POST['name'])) { $this->field_errors = "Please enter your name."; }
if(!$this->addr1 = trim($_POST['addr1'])) { $this->field_errors = "Please enter your address."; }
if(!$this->city = trim($_POST['city'])) { $this->field_errors = "Please enter your city."; }
if(isset($_POST['state'])) { $this->state = $_POST['state']; } // not required
if(isset($_POST['zip'])) { $this->zip = trim($_POST['zip']); } // not required
if(!$this->email = trim($_POST['email'])) { $this->field_errors = "Please enter your email address."; }
if($this->email != $this->email_comp) { $this->field_errors = "The email addresses you entered did not match."; }
if(empty($this->field_errors)) {
return true;
} else {
$session->message("...");
redirect_to('index.php?page_id=5'); // This function is defined in a separate functions file and works in other contexts
}
}
?>
Code: Select all
<?php
class Session {
public $message;
public function message($msg="") {
if(!empty($msg)) {
$_SESSION['message'] = $msg;
} else {
return $this->message;
}
}
private function check_message() {
if(isset($_SESSION['message'])) {
$this->message = $_SESSION['message'];
unset($_SESSION['message']);
} else {
$this->message = "";
}
}
}