Help with Design

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Help with Design

Post by John Cartwright »

Okay well, I've sort of hit a wall on how I can effectively handle this problem. The problem being that I have a controller for a specific portion of my site, which is initialized from the front control, but I want to extend certain functionality to all my controllers. Confused? So am I so lets dig in to some code. Here is the home page controller

Code: Select all

<?php

	#		Contains action methods
	# No need for initializing controller so lets just require it here
	require_once('actionControllers/class.homeActionController.php');	

	class homeController extends homeActionController 
	{		
		#	Action List of exceptions/commands that may be issued 
		#	As specific command
		var $actionRules = array('showpoll');
		#	Exception list of possible exceptions that may be 
		#	Issued to a specific action. 'Action' should not be deleted, 
		#	Just in case but it does not neccesarily exist if the 
		#	Page does not require actions
		var $exceptions  = array('action', 'pollId');
		#	List of safe exceptions and actions only available within
		#	This controller		
		var $rulesValues = array();
		var $methods = array();
		var $httpRequests;

		function homeController() 
		{
			$this->getMethods();
			$this->loadRuleValues();
			$this->initPageLoad();
			$this->loadLazyLibrary();
			$this->execute();	
		}
		
		function loadRuleValues() {
			foreach ($this->methods as $methodType) {
				foreach ($methodType as $name => $value) {
					if ($this->isException($name)) {
						if (!empty($this->methods['GET'][$name])) {
							$this->ruleValues[$name] = $value;
						}
						else if (!empty($this->methods['POST'][$name])) {
							$this->ruleValues[$name] = $value;
						}
					}
				}
			}
		}
		
		function getMethods() {
			$this->httpRequests = Singleton::instance('httpRequest');
			$this->methods['GET']  = $this->httpRequests->GET();
			$this->methods['POST'] = $this->httpRequests->POST();
		}

		#	List of commands that need to be executed
		#	Responsibility of these cases are determined through exceptions
		#	Must add catches for commands aswell as exceptions to change arguments
		function execute() 
		{				
			$this->initPageLoad();
			if ($this->isException('showPoll') && !empty($this->ruleValues['pollId'])) {
				$this->buildPoll($this->rulesValues['pollId']);
			}
			else {
				$this->buildPoll();
			}
			$this->buildNewsDisplay();
			$this->buildNewsLetter();		
			$this->initPageComplete();
		}
					
		#	GET Array is always given priority over POST
		#	Note: We only want to give access to the classes values that have been
		#	Directly mapped to this, just in case
		function isException($inline = '') 
		{	
			if (empty($inline) && in_array($this->ruleValues['action'], $this->exceptions)) {		
				return true;
			}
			else if (in_array($inline, $this->exceptions)) {
				return true;
			}	
		}

		#	Any library files that are required by default for page to work
		#	Defaulted to lazy load
		function loadLazyLibrary() 
		{
			require_once('systemLib/class.pagination.php');
			require_once('systemLib/class.poll.php');
		}
		
		#	Some sections may only require a specific library to accomplish a 
		#	Task, so there is no reason we need to load it so the whole class
		#	Has access to it
		function loadSpecificLibrary($className) {
			require_once('systemLib/class.'.$className.'.php');
		}
	}
	
#

?>
So each page is resented by 2 seperate classes, the controller and the action class (container for specific actions that should take place). As you can see I'm already extending this function to the action class. Now methods such as

Code: Select all

function loadRuleValues() {
		}
		
		function getMethods() {
		}
I feel as if they should not be included in this class.. but have a controller for my controllers. I have a crapload of controllers here don't I.

I want thinking something along the lines of

FrontControl --> MasterControl extends PageControl extends ActionControl.

Sure enough that is easy.. but the implementation is something I cannot figure out.. how do I dynamically extend a specific class onto the MasterControl class? My front control implements the page control through

Code: Select all

function initPageLoad() 
		{
			if ($this->checkPermission()) {
				$page = new $this->pageController;
			}
		}
As you can see, what I want would be to initialize the MasterControl which extends whichver $this->pageController.

Any thoughts? Thanks.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Ermm... Decorator? (sorry, I really don't understand your specifications. And that probably means you're overengineering...)
Post Reply