Skip to content

update php8 course #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ Use the Linux for Composer Tools to create the two config files as follows:
* Copy the `/path/to/repo/templates/creds.json.dist`:
```
cd /path/to/repo
cp templates/creds.json.dist ./creds.json`
cp templates/creds.json.dist ./creds.json
```
* Modify `/path/to/repo/creds.json` as follows:
| Replace ... | With ... |
| :---------- | :------- |
| %FORK%% | ... the URL value represented by `%%FORK%%` which you wrote down in an earlier step above |

| Replace ... | With ... |
| :--------------- | :------- |
| %%FORK%% | ... the URL value represented by `%%FORK%%` which you wrote down in an earlier step above |
| %%GITHUB_EMAIL%% | ... your github.com email address |
| %%GITHUB_NAME%% | ... your github.com account name |
| %%ACCOUNT%% | ... your Linux for PHP Cloud Services account name (all lowercase) |
| %%USER%% | ... your Linux for PHP Cloud Services login name (email address) |
| %%TOKEN%% | ... your Linux for PHP Cloud Services access token |
| %%GITHUB_NAME%% | ... your github.com account name |
| %%ACCOUNT%% | ... your Linux for PHP Cloud Services account name (all lowercase) |
| %%USER%% | ... your Linux for PHP Cloud Services login name (email address) |
| %%TOKEN%% | ... your Linux for PHP Cloud Services access token |

* Run the Linux for Composer Tools command to generate the two config files:
* Commands if you're running on Linux:
```
Expand Down
18 changes: 18 additions & 0 deletions examples/core_cool_mixed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
// core_cool_mixed.php

function old_style($a)
{
return $a;
}

function new_style(mixed $a) : mixed
{
return $a;
}

$reflect[] = new ReflectionFunction('old_style');
$reflect[] = new ReflectionFunction('new_style');

foreach ($reflect as $item) echo $item . "\n";

29 changes: 29 additions & 0 deletions examples/core_cool_mixed_covar_no.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// core_cool_mixed_covar_no.php
// demonstrates violation of LSP rules

class Upper
{
public mixed $phrase;
public function wide(string $a)
{
return base64_encode($a);
}
public function narrow(mixed $a)
{
return base64_decode($a);
}
}
class Middle extends Upper
{
// this does NOT work
public function narrow(string $a)
{
return base64_decode($a);
}
}
class Finger extends Upper
{
// this does NOT work
public string $phrase
}
33 changes: 33 additions & 0 deletions examples/core_cool_mixed_covar_ok.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// core_cool_mixed_covar_ok.php
// demonstrates co-variance support

class Upper
{
public function wide(string $a)
{
return base64_encode($a);
}
public function narrow(mixed $a)
{
return base64_decode($a);
}
}
class Middle extends Upper
{
// this works OK
public function wide(mixed $a)
{
return parent::wide($a);
}
}

$phrase = 'FCF Continuous Learning';
$middle = new Middle();
$encoded = $middle->wide($phrase);
$decoded = $middle->narrow($encoded);

echo "\nOriginal: $phrase\n";
echo "\nBase 64 : $encoded\n";
echo "\nDecoded : $decoded\n";

42 changes: 42 additions & 0 deletions examples/core_cool_mixed_variance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
// core_cool_mixed_variance.php
// demonstrates co-variance support

class Upper
{
public function wide(string $a)
{
return base64_encode($a);
}
public function narrow(mixed $a)
{
return base64_decode($a);
}
}
class Middle extends Upper
{
// this works OK
public function wide(wide $a)
{
return parent::wide($a);
}
}
class Finger extends Upper
{
// this does not work
public function narrow(string $a)
{
return parent::narrow($a);
}
}

$phrase = 'FCF Continuous Learning';
$middle = new Middle();
$finger = new Finger();
$encoded = $middle->wide($phrase);
$decoded = $finger->narrow($encoded);

echo "\nOriginal: $phrase\n";
echo "\nBase 64 : $encoded\n";
echo "\nDecoded : $decoded\n";

24 changes: 24 additions & 0 deletions examples/core_cool_nullsafe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
// core_cool_nullsafe.php

// set up autoloader
include __DIR__ . '/src/Loader.php';
$loader = new Loader(__DIR__ . '/src');

use Application\Entity\Event;
$event_key = 'DOG-LOV-RC-885';
$dsn = 'mysql:host=localhost;dbname=phpcl';
$pdo = new PDO($dsn, 'phpcl', 'password');

// event exists
$event = new Event($pdo, $event_key);
echo 'Event:' . $event->getProp('event_name') . PHP_EOL;
echo 'Hotel:' . $event->getHotel()->getProp('hotelName');
echo "\n";

// event does not exist
$event = new Event($pdo, 'BAD-KEY');
echo 'Event:' . $event?->getProp('event_name') . PHP_EOL;
echo 'Hotel: ' . $event?->getHotel()?->getProp('hotelName');
echo "\n";

20 changes: 20 additions & 0 deletions examples/core_cool_nullsafe_fail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
// core_cool_nullsafe_fail.php
// demonstrates short circuiting

$arr1 = array_combine(range('A','F'), range(111,700,111));
$arrObj = new ArrayObject($arr1);
$arr2 = ['A' => $arrObj, 'B' => NULL];
$anon = new class ($arr2) {
public $prop;
public function __construct($arr)
{
$this->prop = new stdClass();
$this->prop->arr = $arr;
}
};
$arr3 = ['anon' => $anon ];
echo $arr3['anon']->prop->arr['A']['A'];
echo "\n";
echo $arr3['anon']?->prop->arr['B']['A'] ?? 'DEFAULT';
echo "\n";
13 changes: 13 additions & 0 deletions examples/core_cool_static_return.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
// core_cool_static_return.php

// autoloader
include __DIR__ . '/src/Loader.php';
use Application\Sql\Insert;
$loader = new Loader(__DIR__ . '/src');
$insert = new Insert();

echo $insert->into('images')
->columns(['title','image'])
->values(['Apple', '/images/dbierer/apple.png'])
->render();
10 changes: 10 additions & 0 deletions examples/core_cool_variadic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
// core_cool_variadic.php
class Upper {
public function test(int $many, string $parameters, $here) {}
}
class Lower extends Upper {
public function test(...$everything) {}
}
$lower = new Lower();
var_dump($lower);
28 changes: 28 additions & 0 deletions examples/core_cool_weak_map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// core_cool_weak_map.php

// initialize two "expensive" objects
$expensive1 = new ArrayObject(range('A','F'));
$expensive2 = new ArrayObject(range('A','F'));

// store with SplObjectStorage
echo "\nSplObjectStorage Before:\n";
$storage = new SplObjectStorage();
$storage->offsetSet($expensive1, 111);
var_dump($storage);

// the object is not unset as a reference exists
unset($expensive1);
echo "\nSplObjectStorage After:\n";
var_dump($storage);

// store with WeakMap
echo "\nWeakMap Before:\n";
$weakmap = new WeakMap();
$weakmap->offsetSet($expensive2, 222);
var_dump($weakmap);

// the object is allow to go away as only a weak reference exists
unset($expensive2);
echo "\nWeakMap After:\n";
var_dump($weakmap);
18 changes: 18 additions & 0 deletions examples/core_cool_weak_ref.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
// core_cool_weak_ref.php
$obj1 = new ArrayObject([1,2,3]);
$obj2 = $obj1;
$obj3 = new ArrayObject(['A','B','C']);

echo 'If we create a _weak reference_ to $obj` and then destroy it,' . PHP_EOL;
echo 'it doesn\'t go away because of `$obj2`:' . PHP_EOL;
$weakref = WeakReference::create($obj1);
var_dump($weakref->get()); // object exists
unset($obj1);
var_dump($weakref->get()); // object exists

echo "\n" . 'On the other hand, the weak reference to `$obj3` goes away after calling `unset()`' . "\n";
$weakref = WeakReference::create($obj3);
var_dump($weakref->get()); // object exists
unset($obj3);
var_dump($weakref->get()); // NULL
2 changes: 2 additions & 0 deletions examples/core_ext_curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
echo "This works in PHP 7\n";
} elseif ($ch instanceof CurlHandle) {
echo "This works in PHP 8\n";
$reflect = new ReflectionObject($ch);
echo $reflect . "\n";
}
24 changes: 24 additions & 0 deletions examples/core_ext_datetime_zone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
// core_ext_datetime_zone.php

date_default_timezone_set('America/New_York');

// forward clock changes
$date[] = new DateTime('2021-03-14 01:30:00');
$date[] = new DateTime('2021-03-14 02:30:00');
$date[] = new DateTime('2021-03-14 03:30:00');

// backwards clock changes
$date[] = new DateTime('2021-11-07 01:30:00');

try {
$date[] = new DateTime('2021-11-07 01:30:00 ST');
$date[] = new DateTime('2021-11-07 01:30:00 DST');
} catch (Throwable $t) {
echo get_class($t) . ':' . $t->getMessage();
echo "\n";
}

foreach ($date as $obj)
echo $obj->format('Y-m-d H:i:s T') . "\n";

14 changes: 14 additions & 0 deletions examples/core_ext_datetimeinterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
// core_ext_datetimeinterface.php

$dt[] = $obj = new DateTime('now');
$dt[] = $imm = new DateTimeImmutable('now');

try {
$dt[] = DateTime::createFromImmutable($imm);
$dt[] = DateTimeImmutable::createFromInterface($dt);
} catch (Throwable $t) {
echo get_class($t) . ':' . $t->getMessage();
echo "\n";
}
var_dump($dt);
40 changes: 17 additions & 23 deletions examples/core_imp_expr_new.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
<?php
// core_imp_expr_new.php
class Base {
public array $data;
public function __construct($data = []) {
$this->data = $data;
}
// generate_primes.php
$start = microtime(TRUE);
$max = 100000;
for ($x = 5; $x < $max; $x++) {
// This if evaluation checks to see if number is odd or even
$test = TRUE;
for($i = 3; $i < $x; $i++) {
if(($x % $i) === 0) {
$test = FALSE;
break;
}
}
if ($test) echo $x . ', ';
}
class JsonResponse extends Base {
public function render() {
header('Content-Type: application/json');
return json_encode($this->data);
}
}
class TextResponse extends Base {
public function render() {
header('Content-Type: text/html');
return var_export($this->data, TRUE);
}
}
$accept = $argv[1] ?? 'text';
$data = ['A' => [1,2,3],'B' => [4,5,6],'C' => [7,8,9]];
$strategies = ['text' => 'TextResponse','json' => 'JsonResponse'];
$response = new ($strategies[$accept] ?? 'JsonResponse')($data);
echo $response->render();
echo "all prime numbers\n";
$end = microtime(TRUE);
$run = ($end - $start) / 1000000;
echo "Run Time: $run seconds\n";
Loading