*/ class mpWebView { /** * The input field text. * * @type string * @var string $text */ private $text = ""; /** * List of event callbacks. * * @type array * @var array $callbacks */ private $callbacks = array(); /** * The view ctor. */ public function __construct() { $this->setText( isset( $_POST["input"] ) ? $_POST["input"] : "" ); } /** * Connects the given callback to $signal. * * @param string $signal * @param string|array $callback */ public function connect( $signal, $callback ) { if ( is_callable( $callback ) ) { if ( !isset( $this->callbacks[$signal] ) ) { $this->callbacks[$signal] = array(); } $this->callbacks[$signal][] = $callback; } } /** * Returns the view text. * * @return string */ public function getText() { return $this->text; } /** * Sets the view text. * * @param string $text */ public function setText( $text ) { $this->text = htmlspecialchars( $text, ENT_QUOTES, "UTF-8" ); } /** * Executes possible events and shows the view. */ public function show() { // Handle events $this->processCallbacks(); printf( ' MVC Example

MVC Example

Please enter a new product name.

', str_replace( getenv( "DOCUMENT_ROOT" ), "", getenv( "SCRIPT_FILENAME" ) ), $this->text ); } /** * Executes callbacks for events */ private function processCallbacks() { // Search for a valid callback foreach ( $this->callbacks as $signal => $callbacks ) { if ( !isset( $_POST[$signal] ) ) { continue; } foreach ( $callbacks as $callback ) { call_user_func( $callback ); } } } }