I’m working on a site that I did not originally program. I’m new to working with PHP classes.
There is a class on this website called Reporting and an extension called main. An instance of main (called $main) is created in the index page (all other pages are an include of the index page). I can’t find any place that an instance of reporting is created.
What confuses me is from what I’ve read, it’s my understanding, in order to access a class you must create an instance of that class like this
[code=php]<$main = new main()
Then you can reference aspects of main like this
[code=php]$main->displaypage()
However there are places all over the code for this site where functions and variables in the reporting class are referenced using a variable called $this, like this
[code=php]$this->set_value(“type”,$_POST[‘type’])
from what I can tell $this is a special variable intended to be used within the class it’s self to reference things within the class.
Is this an appropriate use of $this?
everything seems to work ok (but I’m concenred there’s a problem I’m not seeing or I’ll break something if I don’t understand it)
In order for this to be working, does that mean $this was created as an instance of the reporting calss some where?
Any explination is appericates.
I’m including the classes and an example refernce of this outside the clase incase that will help.
[code=php]
class reporting {
var $errors = array(), $values = array(), $success = array();
function set_error ($name, $message) {
$this->errors[$name] = $message;
}
function get_error ($name) {
return isset($this->errors[$name]) ? $this->errors[$name] : false;
}
function set_success ($name, $message) {
$this->success[$name] = $message;
}
function get_success ($name) {
return isset($this->success[$name]) ? $this->success[$name] : false;
}
function set_value ($name, $value) {
$this->values[$name] = $value;
}
function get_value ($name) {
return isset($this->values[$name]) ? $this->values[$name] : false;
}
function has_errors ($error_list = false) {
if (is_array($error_list)) {
foreach ($error_list as $error) {
if ($this->errors[$error]) {
return true;
}
}
return false;
} else {
return (bool) count($this->errors);
}
}
function remove_error ($error) {
unset($this->errors[$error]);
}
function list_errors () {
return $this->errors;
}
}
class main extends reporting {
var $include, $logged_in, $user = array(), $unread_messages = 0, $options = array();
function main () {
@mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD) && @mysql_select_db(MYSQL_DATABASE) || die(‘<code><strong>MySQL Error:</strong> ‘ . mysql_error() . ‘</code>’);
session_start();
$this->authorize_user();
$this->clean_input();
$page = $_GET[‘page’];
if (preg_match(‘#^[a-z0-9-_]+$#’, $page)) {
if (file_exists(‘php/’ . ($file = ‘action.’ . $page . ‘.php’))) {
include $file;
}
}
$sql = “SELECT * FROM `options`”;
$q = mysql_query($sql);
while ($r = mysql_fetch_assoc($q)) {
$this->options[$r[‘option_name’]] = $r[‘option_value’];
}
if ($this->logged_in) {
$this->unread_messages = @mysql_num_rows(mysql_query(“SELECT `message_id` FROM `messages` WHERE `message_receiver_id` = ‘$this->user[user_id]’ AND `message_status` = ‘0’”));
}
if (preg_match(‘#^[a-z0-9-_]+$#’, $page)) {
if (file_exists(‘php/’ . ($file = ‘page.’ . $page . ‘.php’))) {
$this->include = $file;
} else {
$this->include = ‘page.error.php’;
}
} elseif (!strlen($page)) {
$this->include = ‘page.home.php’;
} else {
$this->include = ‘page.error.php’;
}
ob_start(array(&$this, ‘relatize’));
}
function get_option ($option_name) {
$sql = “SELECT * FROM `options` WHERE `option_name` = ‘$option_name'”;
$q = mysql_query($sql);
$r = mysql_fetch_assoc($q);
return unserialize($r[‘option_value’]);
}
function update_option ($option_name, $option_value) {
$option_value = is_array($option_value) ? serialize($option_value) : $option_value;
$sql = “UPDATE `options` SET `option_value` = ‘$option_value’ WHERE `option_name` = ‘$option_name'”;
$q = mysql_query($sql);
}
function display_page () {
include $this->include;
}
function allow_only ($types) {
if (is_array($types)) {
if (!in_array($this->user[‘user_type’], $types)) {
header(‘Location: ‘ . SITE_ROOT);
exit;
}
} else {
if ($this->user[‘user_type’] != $types) {
header(‘Location: ‘ . SITE_ROOT);
exit;
}
}
}
function authorize_user () {
if (valid_user_cookies()) {
$details = user_details($_COOKIE[‘user_id’]);
if ($details[‘user_password’] == $_COOKIE[‘user_pass’]) {
$this->logged_in = true;
$this->user = $details;
$sql = “UPDATE `users` SET `user_timestamp_last` = UNIX_TIMESTAMP(NOW()) WHERE `user_id` = ‘$details[user_id]'”;
mysql_query($sql);
} else {
$this->destroy_user();
}
}
}
function destroy_user () {
$time = time() – (60 * 60 * 24 * 7);
$parts = parse_url(SITE_ROOT);
setcookie(‘user_id’, ‘NULL’, $time, $parts[‘path’], $parts[‘host’]);
setcookie(‘user_pass’, ‘NULL’, $time, $parts[‘path’], $parts[‘host’]);
$this->logged_in = false;
}
function clean_input () {
foreach ($_GET as $key => $value) {
if (is_string($value)) {
$value = trim($value);
$_GET[$key] = get_magic_quotes_gpc() ? stripslashes($value) : $value;
}
}
foreach ($_POST as $key => $value) {
if (is_string($value)) {
$value = trim($value);
$_POST[$key] = get_magic_quotes_gpc() ? stripslashes($value) : $value;
}
}
}
function relatize ($buffer) {
return preg_replace(‘#(href|src|action)=”/#’, ‘\1=”‘ . SITE_ROOT, $buffer);
}
}
//from a file used to handle input from front end page
if (!is_uploaded_file($_FILES[‘file’][‘tmp_name’]) || !is_image($_FILES[‘file’][‘tmp_name’])) {
$this->set_error(‘file’, ‘Please select a valid image to upload.’);
}