Code Examples

A few snippets of code demonstrating use and understanding of PHP5 OOP

These snippets come from a project that required a highly configurable, lightweight, single platform without depending on a database such as MySQL. The code builds configuration data using a factory class in order to decouple the components as much as possible.

<?php
 
/**
 * SchemeConfigCreator
 *
 * defines the interfaces required for new SchemeConfigCreators
 *
 * @see js/config.js
 * @see access_control.php
 *
 */
interface SchemeConfigCreator {
 
	/**
	 * @param Scheme $obj the empty scheme configuration to be populated with correct configuration
	 */
	public function createConfig(Scheme $obj);
 
}
?>
<?php
/**
 * SchemeConfigData
 *
 * extends SchemeConfigData to add the ability to construct a scheme configuration using a Factory class
 */
class Scheme extends SchemeConfigData {
 
	/**
	 * @param SchemeConfigCreator $creator the Factory object to use to create the configuration data
	 * @return Scheme a populated scheme configuration
	 */
	public function createConfig(SchemeConfigCreator $creator) {
		//krumo ($creator);
		return $creator->createConfig($this);
	}
}
?>
<?php
 
/**
 * SchemeConfig_Factory
 *
 * Creates SchemeConfigs based on chosen scheme
 *
 * @author Russell Hutson
 */
class SchemeConfig_Factory {
 
	/**
	 * @parama string $schemename a scheme name
	 * @return SchemeConfig a scheme config object depending on the scheme
	 * @throws Unsupported Scheme
	 */
 	public static function getSchemeConfigCreator($schemename) {
        // construct our class name and check its existence
        $class = $schemename . '_SchemeConfig';
        if(class_exists($class)) {
            // return a new Config object
            //echo "$class Exists!<br />";
            return new $class();
        }
        // otherwise we fail
        throw new Exception('Unsupported scheme');
    }
}
?>
<?php
 
/**
 * demoems_SchemeConfig
 *
 * Create schemeConfig for the demoems scheme.
 *
 * @see js/config.js
 * @see access_control.php
 *
 */
class demoems_SchemeConfig implements SchemeConfigCreator {
 
	/**
	 * @param		Scheme $obj the empty scheme configuration to be populated with correct configuration
	 * @return	Scheme the scheme configuration object, fully populated
	 */
	public function createConfig(Scheme $obj) {
 
			// Set some variables for use in afterorderdialog.php (i.e. template variables)
			$obj->setSchemetype('mobile'); // NEED TO IMPLEMENT SETTER IN SchemeConfigData and use SeTTERs here
			$obj->setCompanyname('Demo EMS');
			$obj->setCompanynameabbrev('Demo EMS');
			$obj->setCompanyaddress('[COMPANY ADDRESS]');
			$obj->setAgreementpostaladdress('[COMPANY POSTAL ADDRESS]');
			$obj->setSchemename('Demo EMS');
			$obj->setCompanyregno('[CO.REG.NO]');
			$obj->setCompanyvat('[CO.VAT.NO]');
			$obj->setSchemeroot('demo');
 
			//$obj->setschemeterms('scheme_terms_as_html_mobile_multinetwork_wet.inc.php');
			$obj->setSchemeterms('generic-mobile-hire-agreement-final.inc.php');
 
			$obj->setTagline("The Latest Phones. Fantastic Deals. Great Savings!");
 
 
			$obj->setEmaillogotop('demo-emaillogo.jpg');
			$obj->setClosingdate('December 31st, 2011');
			$obj->setSchemestatus($obj->determine_schemestatus('_live', $obj->getClosingdate(), 17));
 
 
 
			// See ems_init.js/info_popups_init() regarding obfuscated email addresses
			$obj->setHelp_email('salarysacrifice splidge micro-p splodge com');
 
			$obj->setJqueryuicss('js/latestjquery/css/demo/jquery-ui-1.8.13.custom.css');
			$obj->setJqueryuijs('js/latestjquery/js/jquery-ui-1.8.13.custom.min.js');
			$obj->setJqueryjs('js/latestjquery/js/jquery-1.5.1.min.js');
 
			$obj->setOtherscheme('demoecs');
			$obj->setFlexbenkey('sssdemo');
			$obj->setFormid(99); // disabled in config.js/features
 
			return $obj;
 
	}
 
}
?>