Composer, Traits, Generators, PRG




CS174

Chris Pollett

Mar 15, 2017

Outline

Composer

Composer

Example Composer Project Using Yioop

Comments on the Example

Traits

Exceptions

Generators

Variable and Anonymous Functions

Our Website Design Paradigm So Far...

POST REDIRECT GET

Let's Build Something

Consider the following three files worth of code:

<?php
// View.php
abstract class View
{
    public $layout;
    public function __construct(string $layout) 
    {
        $this->layout = new $layout($this);
    }
    public final function display($data = [])
    {
        $this->layout->renderHeader($data);
        $this->render($data);
        $this->layout->renderFooter($data);
    }
    public abstract function render($data = []);
}
<?php
//Layout.php
abstract class Layout
{
    public $view;
    public function __construct(View $view)
    //notice type hinting allowed for objects
    {
        $this->view = $view;
    }
    public abstract function renderHeader($data);
    public abstract function renderFooter($data);
}
<?php
//index.php
require_once "LetsBuildView.php";
require_once "WebLayout.php";

$data['title'] = "Let's Build Something";
$data['content'] = "<p>Yippee! It works!</p>";
$data['content'] .= "<p>I should figure out how to get this to work with controllers</p>";
$view = new LetsBuildView("WebLayout");
$view->display($data);

Assume the file index.php is the entry point into the above program. Create concrete implementations of the classes View and Layout called LetsBuildView and WebLayout such that the output of the above is an HTML 5 web page with title $data['title'], the body of the page has $data['title'] in an h1 tag, and has $data['content'] in a paragraph tag.

In the above replace "I should figure out how to get this to work with controllers" with a sentence or two about how to get this to work with controllers.

Post your solutions to the Mar. 15 In-Class Exercise Thread