CS174
Chris Pollett
Oct 19, 2016
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" } }
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'] = "Yippee! It works!"; $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.
Post your solutions to the Oct. 19 Let's Build Something Thread