CS174
Chris Pollett
Mar 15, 2017
use namespace;
declaration can be used
together with a registered autoload callback function (registered via spl_autoload_register) to autoload classes.composer some_directive_for_composer
{ "name": "your_vendor_name/your_project", "description": "put what your project is about here", "authors": [ { "name": "Your Full Name", "email": "Where@to_contact.you" } ], "require": { "seekquarry/yioop": ">=3.8.0" } }
{ "name": "your_vendor_name/your_project", "description": "put what your project is about here", "authors": [ { "name": "Your Full Name", "email": "Where@to_contact.you" } ], "require": { // whatever the requires your project needs }, "autoload": { "psr-4": { "my_company\\my_project\\": ["src/"] } } }
namespace cpollett\test_composer; use seekquarry\yioop\configs as SYC; use seekquarry\yioop\library as SYL; use seekquarry\yioop\library\PhraseParser; require_once "vendor/autoload.php"; print_r(PhraseParser::stemTerms("jumping", 'en-US')); echo SYL\crawlHash(SYC\NAME_SERVER . "1234");
composer installIf you want to update composer projects later you type:
composer update
php index.phpto execute out project. It should output:
Array ( [0] => jump ) joQWpC4Sdy4
trait MyMethods { function getFoo() { /* code for getFoo */ } function getGoo() { /* code for getFoo */ } } class SomeClassA extends NeedsMethods { use MyMethods; //as if I had written the code in MyMethods here /* rest of SomeClassA code */ } class SomeClassB extends NeedsMethods { use MyMethods; /* rest of SomeClassB code */ } $a = new SomeClassA(); $a->getFoo();
class A { use MyMethodsA, MyMethodsB; // ... }
use MyMethodsA, MyMethodsB { MyMethodsA::foo insteadof MyMethodsB; MyMethodsB::goo insteadof MyMethodsA; MyMethodsA::goo as gooey; MyMethodsB::foo as protected fooey; //notice can change visibility if want }
try {} catch(MyException $e){} catch(Exception $ee){}
if($denom == 0){ throw new Exception("divide by zero"); }
function my_gen($start, $end) { for ($i = $start; $i < $end; $i++) { // $i is preserved between yields. yield $i; } } $generator = my_gen(0, 10000); foreach ($generator as $value) { echo "$value\n"; }
yield $k => $v;
$action = $_REQUEST['a']; $legal_actions = ['landing', 'write', 'read']; if (in_array($action, $legal_actions)) { $action(); // here is the variable function }
$salutation = function($name) { echo "Hi there, " . $name; } $salutation('Chris'); $salutation('Bob'); /* To refer to values from the parent scope you can do: (i.e., this shows PHP has closures) */ $msg = 'there'; // Closures can also accept regular arguments $example = function ($arg) use ($msg) { echo $arg . ' ' . $msg; }; $example("hi");
<form method="post"> <!--... some form elements --> <input type="submit" name='submit button' value='submit' /> </form>we process the data in the receiving controller. Rather than choose a view and display it, we instead, output a 301 redirect, location header to some safe page. In PHP, we could do this with a line like:
<?php header("Location:http://somewhere.safe.com/");
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