PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 12 ■ ENTERPRISE PATTERNS


The Request object is also a useful repository for data that needs to be communicated to the view
layer. In that respect, Request can also provide response capabilities.
Here is a simple Request class:


namespace woo\controller;
//...


class Request {
private $properties;
private $feedback = array();


function __construct() {
$this->init();
\woo\base\RequestRegistry::setRequest($this );
}


function init() {
if ( isset( $_SERVER['REQUEST_METHOD'] ) ) {
$this->properties = $_REQUEST;
return;
}


foreach( $_SERVER['argv'] as $arg ) {
if ( strpos( $arg, '=' ) ) {
list( $key, $val )=explode( "=", $arg );
$this->setProperty( $key, $val );
}
}
}


function getProperty( $key ) {
if ( isset( $this->properties[$key] ) ) {
return $this->properties[$key];
}
}


function setProperty( $key, $val ) {
$this->properties[$key] = $val;
}


function addFeedback( $msg ) {
array_push( $this->feedback, $msg );
}


function getFeedback( ) {
return $this->feedback;
}


function getFeedbackString( $separator="\n" ) {
return implode( $separator, $this->feedback );
}
}

Free download pdf