CS174
Chris Pollett
Oct 24, 2022
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": "dev-master" } }
{ "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
Which of the following statements is true?
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");
<?php $fiber = new Fiber(function() : void { $value = Fiber::suspend('fiber'); echo "Value used to resume fiber: ", $value, PHP_EOL; }); $value = $fiber->start(); echo "Value from fiber suspending: ", $value, PHP_EOL; $fiber->resume('test');
Value from fiber suspending: fiber Value used to resume fiber: test