Skip to content

Updates from PHP8 course #1

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 7 commits into from
Sep 17, 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
20 changes: 9 additions & 11 deletions examples/core_lab_oop_diffs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
define('SRC_DIR', __DIR__ . '/../labs/oop_diffs/src');
$autoload = function ($class) {
$fn = SRC_DIR . '/'
. str_replace('\\', '/', $class)
. '.php';
require_once $fn;
};
$data = ['A' => [1,2,3],'B' => [4,5,6],'C' => [7,8,9]];
try {
spl_autoload_register('autoload', TRUE);
} catch (Exception $e) {
include SRC_DIR . '/Loader.php';
$loader = new Loader(SRC_DIR);
spl_autoload_register('Loader');
} catch (Exception $e) {
include SRC_DIR .'/autoload.php';
}
$response = new Response($data);
echo 'JSON Response: ' . $response->render();

use Application\Strategy\JsonResponse;
$response = new JsonResponse();
$response->one = $data;
echo 'Version : ' . $response::version();
echo "\nJSON Response : {$response->one}\n";
29 changes: 29 additions & 0 deletions examples/core_lab_proc_diffs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// core_lab_proc_diffs.php
ini_set('display_errors', 0);
define('SRC_DIR', __DIR__ . '/../labs/proc_diffs');
define('ERR_AGE', 'Invalid Age');
define('ERR_ZERO', 'Zero');

// validate staff
$data = include SRC_DIR . '/staff.php';
foreach ($data as $key => $row) {
$err = '';
if (!is_numeric($row{2})) {
$err = ERR_AGE;
}
if ($row{2} == 0) {
$err .= ' ' . ERR_ZERO;
}
$row[] = trim($err);
$row{2} = $row{2} + 1;
$data[$key] = $row;
}

// list employees
echo "\nCEO: {$data[0][1]}\n";
echo "ID\tName\tAge\tTitle\tErrors\n";
foreach ($data as $row) {
echo implode($row, "\t") . "\n\t";
echo $row[2] + 10 . " is his/her age in 10 years\n";
}
18 changes: 14 additions & 4 deletions examples/core_proc_data_wrapper.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?php
// core_proc_data_wrapper.php
// ??? maybe there's a better example ???
$dsn = 'mysql:host=localhost;dbname=phpcl';
$pdo = new PDO($dsn, 'phpcl', 'password');
$stmt = $pdo->query("SELECT * FROM images WHERE title = 'Apple'");
$stmt = $pdo->query('SELECT * FROM images');
$titles = [];
echo '<table>';
echo '<tr>';
if ($stmt) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['title'] . "\n";
echo '<img src="' . "data://image/jpeg;base64,{$row['image']}" . '" />';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$titles[] = $row['title'];
echo '<td>'
. '<img src="' . "data://image/jpeg;base64,{$row['image']}" . '" />'
. '</td>';
}
}
echo '</tr>';
echo '<tr><th>' . implode('</th><th>', $titles) . '</th></tr>';
echo '</table>';
6 changes: 3 additions & 3 deletions examples/core_proc_num_to_str.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
$mixed = '42abc';
$array = ['A','B','C'];

$result = ($value == $string) ? 'is the same as' : 'is not the same as';
$result = ($value == $string) ? 'is the same as' : 'is NOT the same as';
echo 'The value "' . $value . '" ' . $result . ' "' . $string. "\"\n";

$result = (in_array($value, $array)) ? 'is in' : 'is not in';
$result = (in_array($value, $array)) ? 'is in' : 'is NOT in';
echo 'The value "' . $value . '" ' . $result . "\n" . var_export($array, TRUE);

$result = ($mixed == 42) ? 'is the same as' : 'is not the same as';
$result = ($mixed == 42) ? 'is the same as' : 'is NOT the same as';
echo "\n" . 'The value "' . $mixed . '" ' . $result . " 42\n";
20 changes: 11 additions & 9 deletions examples/core_proc_numeric_strings.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
// core_proc_numeric_strings.php
$test = ['111', ' 111', '111 ', '111doug'];
foreach ($test as $numStr)
var_dump((int) $numStr);
function test(float $i) {
return var_export($i, TRUE);
return var_export($i, TRUE) . "\n";
}
foreach ($test as $numStr) {
test($numStr);
echo "\n";
try {
foreach ($test as $numStr) test($numStr);
} catch (Throwable $t) {
echo $t;
}
foreach ($test as $numStr) {
var_dump(111 + $numStr);
echo "\n";

try {
foreach ($test as $numStr)
var_dump(111 + $numStr, TRUE);
} catch (Throwable $t) {
echo $t;
}
31 changes: 31 additions & 0 deletions examples/core_proc_serialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
// core_proc_serialize.php
define('TARGET_DIR', __DIR__ . '/data');
class Test
{
public $arr = [];
public $obj1 = NULL;
public $obj2 = NULL;
}

// build up object
$arr = ['A' => 111, 'B' => 222, 'C' => 333];
$test = new Test();
$test->arr = $arr;
$test->obj1 = new ArrayObject($arr);
$test->obj3 = new ArrayIterator($arr);

// store serialized version based on PHP vers
$phpFile = TARGET_DIR . '/serial_' . PHP_VERSION . '.txt';
$str = serialize($test);
echo $str . "\n";
file_put_contents($phpFile, $str);

// unserialize all files in TARGET_DIR
$list = glob(TARGET_DIR . '/serial_*');
foreach ($list as $fn) {
$restored = file_get_contents($fn);
$obj = unserialize($restored);
echo "\n" . basename($fn) . "\n";
var_dump($obj);
}
3 changes: 3 additions & 0 deletions examples/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Placeholder

This directory exists for examples to write temporary data.
Binary file added images/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/data_01_person.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/data_02_woman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/data_03_man.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/data_04_camera.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/data_05_tv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/regular_exp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/strawberry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'proc' => 'Procedural PHP',
'ext' => 'PHP Extensions',
'error' => 'Error Handling',
'labs' => 'Labs',
'lab' => 'Labs',
];
// output file list
if (empty($message)) $message = '';
Expand Down
20 changes: 14 additions & 6 deletions labs/oop_diffs/src/Application/Strategy/JsonResponse.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php
namespace Application\Strategy;
namespace Application\ Strategy;
class JsonResponse
{
public function jsonresponse($data = [])
use VersionTrait;
const VERSION = '1.0';
protected $internal = [];
public function __set($key, $value)
{
$this->data = $data;
$this->internal[$key] = $value;
}
public function render()
public function __get($key) : string
{
header('Content-Type: application/json');
return json_encode($this->data);
$json = $this->internal[$key] ?? [];
return json_encode($json);

}
public function version() : string
{
return self::VERSION;
}
}
6 changes: 6 additions & 0 deletions labs/oop_diffs/src/Application/Strategy/VersionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Application\ Strategy;
trait VersionTrait
{
public abstract function version(int $num) : int;
}
9 changes: 6 additions & 3 deletions labs/oop_diffs/src/Loader.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php
class Loader
{
protected $dir = '';
public function loader($dir)
{
$this->dir = $dir;
}
public function __invoke($class)
{
$fn = SRC_DIR . '/'
$fn = $this->dir . '/'
. str_replace('\\', '/', $class)
. '.php';
require_once $fn;
}
}
$loader = new Loader();
spl_autoload_register($loader);
8 changes: 8 additions & 0 deletions labs/oop_diffs/src/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
function __autoload($class)
{
$fn = SRC_DIR . '/'
. str_replace('\\', '/', $class)
. '.php';
require_once $fn;
}
4 changes: 4 additions & 0 deletions labs/proc_diffs/staff.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 Andrew 32 CEO
2 Doug 63doug CTO
3 Andree-Anne 28 Senior DevOp
4 Nicole 46 Partner
9 changes: 9 additions & 0 deletions labs/proc_diffs/staff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
return [
-2 => [5,'James','thirty','Mktg'],
[1,'Andrew','32','CEO'],
[2,'Doug','63doug','CTO'],
[3,'Anne',' 28','DevOp'],
[4,'Nicole','46 ','Partner'],
[6,'Fred','999','Caveman'],
];
Loading