may 23, 2009 08:21pm

PHP Service Class

Part of my work, towards creating the AI, is improving my tool set. I've already spent a fair amount of time working on the blog, and software behind the scenes for administration. As such I released phpSimpleMyAdmin this week, which I hope to improve upon.

Also, I'm looking at improving a few of my sites with unobstrusive JQuery and "interesting" PHP web services (not the formal XML kind mind), but I revisited an old framework earlier and repurposed it into something simpler. I post it here as a template for future work.

<?php

$service = new Service();

class Service
{

	var $action;
	var $session;
	var $output;

	function Service()
	{
		$this->StartSession();
		$this->ProcessAction();
		$this->DisplayOutput();
		$this->SaveSession();
	}
	
	function StartSession()
	{
		session_start();
		$this->session = $_SESSION['Service'];
	}

	function ProcessAction()
	{
		$action = trim($_GET['action']);
		$method = 'action_'.$action;
		
		if($action == false)
		{
			$this->output = 'No action defined. ';
		}
		else
		if(method_exists($this, $method))
		{
			$this->{$method}();
		}
		else
		{
			$this->output = 'The action '.$action.' is not a valid service request.';
		}
		
		$this->session['count']++;
	}
	
	function action_RandomMessage()
	{
		$quotes = array('John is a creative individual', 'Haruhi is god', 'Kyonko is love', 'Lucky Star is slated by many', 'Meatloaf is a bat out of hell');
		$this->output = $quotes[rand(0, count($quotes)-1)];
	}
	
	function DisplayOutput()
	{
		echo $this->output . ' (' . $this->session['count'] . ') ';
	}
	
	function SaveSession()
	{
		$_SESSION['Service'] = $this->session;
	}

}
?>


Edit: Example of this working: JQuery Quote Picker.

Posted By: Markavian

2 comments | Comments