Jump to content

First hand at classes...efficiency?


DjMikeS

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.