Jump to content

Recommended Posts

Hi all...

 

I've read al lot about classes, but have been able to grasp it. So I decided to just start using it and see where it ends...

 

I've written a class which, at the moment, is nothing more then a collection of functions. I would like to know if it can be done more efficient or 'nicer'....

 

Here's the code:

<?php
class nagiosStatus {
function getLastUpdate() {
	$getLastUpdate = "SELECT id,timestamp FROM nagios_program ORDER BY id DESC LIMIT 0,1";
	try {
		if (!$qResult = mysql_query($getLastUpdate)) {
			$mysql_error = mysql_error();
			throw new Exception($mysql_error);
			return false;
		}
		else {
			$arrLastUpdate = mysql_fetch_array($qResult);
			return $arrLastUpdate;
		}
	}
	catch (Exception $e) {
		$error = $e->getMessage();
		$script = $e->getFile();
		log_error($error, $script);
		return "An unexpected error has occured. This error has been logged and will be investigated as soon as possible.";
	}
}
function getHosts($intTid) {
	$getLastHosts = "SELECT * FROM nagios_hosts WHERE tid = '$intTid'";
	try {
		if (!$qResult = mysql_query($getLastHosts)) {
			$mysql_error = mysql_error();
			throw new Exception($mysql_error);
			return false;
		}
		else {
			$i = 0;
			$arrLastHosts = array();
			while($row = mysql_fetch_array($qResult)) {
				while(list($myVariableName,$sqlFieldName)=each($row)) {
					$arrLastHosts[$i][$myVariableName] = $sqlFieldName;
				}
			$i++;
			}
			return $arrLastHosts;
		}
	}
	catch (Exception $e) {
		$error = $e->getMessage();
		$script = $e->getFile();
		log_error($error, $script);
		return "An unexpected error has occured. This error has been logged and will be investigated as soon as possible.";
	}
}
function getServices($intTid) {
	$getLastServices = "SELECT * FROM nagios_services WHERE tid = '$intTid'";
	try {
		if (!$qResult = mysql_query($getLastServices)) {
			$mysql_error = mysql_error();
			throw new Exception($mysql_error);
			return false;
		}
		else {
			$i = 0;
			$arrLastServices = array();
			while($row = mysql_fetch_array($qResult)) {
				while(list($myVariableName,$sqlFieldName)=each($row)) {
					$arrLastServices[$i][$myVariableName] = $sqlFieldName;
				}
			$i++;
			}
			return $arrLastServices;
		}
	}
	catch (Exception $e) {
		$error = $e->getMessage();
		$script = $e->getFile();
		log_error($error, $script);
		return "An unexpected error has occured. This error has been logged and will be investigated as soon as possible.";
	}
}
function getServiceHistory($intTid, $host, $service) {
	$getServiceHistory = "SELECT * FROM nagios_services WHERE host = '$host' AND service = '$service' GROUP BY last_check";
	try {
		if (!$qResult = mysql_query($getServiceHistory)) {
			$mysql_error = mysql_error();
			throw new Exception($mysql_error);
			return false;
		}
		else {
			$i = 0;
			$arrServiceHistory = array();
			while($row = mysql_fetch_array($qResult)) {
				while(list($myVariableName,$sqlFieldName)=each($row)) {
					$arrServiceHistory[$i][$myVariableName] = $sqlFieldName;
				}
			$i++;
			}
			return $arrServiceHistory;
		}
	}
	catch (Exception $e) {
		$error = $e->getMessage();
		$script = $e->getFile();
		log_error($error, $script);
		return "An unexpected error has occured. This error has been logged and will be investigated as soon as possible.";
	}
}
function getHostHistory($intTid, $host) {
	$getHostHistory = "SELECT * FROM nagios_hosts WHERE host = '$host' GROUP BY last_check";
	try {
		if (!$qResult = mysql_query($getHostHistory)) {
			$mysql_error = mysql_error();
			throw new Exception($mysql_error);
			return false;
		}
		else {
			$i = 0;
			$arrHostHistory = array();
			while($row = mysql_fetch_array($qResult)) {
				while(list($myVariableName,$sqlFieldName)=each($row)) {
					$arrHostHistory[$i][$myVariableName] = $sqlFieldName;
				}
			$i++;
			}
			return $arrHostHistory;
		}
	}
	catch (Exception $e) {
		$error = $e->getMessage();
		$script = $e->getFile();
		log_error($error, $script);
		return "An unexpected error has occured. This error has been logged and will be investigated as soon as possible.";
	}
}
}

 

I use it like this:

<?php
$nagiosStatus = New nagiosStatus();
$arrLastUpdate = nagiosStatus::getLastUpdate();
$arrLastHosts = nagiosStatus::getHosts($arrLastUpdate[0]);
$arrLastServices = nagiosStatus::getServices($arrLastUpdate[0]);

//Do stuffy here....
?>

 

I appreciate any thoughts about it :)

Link to comment
https://forums.phpfreaks.com/topic/141435-first-hand-at-classesefficiency/
Share on other sites

The point of OOP is not to just simply wrap procedural code into a bunch of static functions.  You might as well just have getLastUpdate(), getHosts(), etc. just as functions if that's how you're going to do it.  OOP allows you to emulate real-world objects and relations and structure your code around that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.