Skip to content

Commit 96a6ba3

Browse files
author
dbierer
committed
2026-01-18-13-01-23 DB: Updated code for chapter 3
1 parent 39cad04 commit 96a6ba3

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace Library;
3+
use DateTime;
4+
include (__DIR__ . DIRECTORY_SEPARATOR . 'ch03_developing_func_type_hints_library.php');
5+
6+
/*
7+
8+
try {
9+
$callable = function () { return 'Callback Return'; };
10+
echo someTypeHint([1,2,3], new DateTime(), $callable);
11+
echo someTypeHint('A', 'B', 'C');
12+
} catch (TypeError $e) {
13+
echo $e->getMessage();
14+
echo PHP_EOL;
15+
}
16+
17+
try {
18+
echo someScalarHint(TRUE, 11, 22.22,
19+
'This is a string');
20+
echo someScalarHint('A', 'B', 'C', 'D');
21+
} catch (TypeError $e) {
22+
echo $e->getMessage();
23+
}
24+
*/
25+
26+
try {
27+
// positive results
28+
$b = someBooleanHint(TRUE);
29+
$i = someBooleanHint(11);
30+
$f = someBooleanHint(22.22);
31+
$s = someBooleanHint('X');
32+
var_dump($b, $i, $f, $s);
33+
// negative results
34+
$b = someBooleanHint(FALSE);
35+
$i = someBooleanHint(0);
36+
$f = someBooleanHint(0.0);
37+
$s = someBooleanHint('');
38+
var_dump($b, $i, $f, $s);
39+
} catch (TypeError $e) {
40+
echo $e->getMessage();
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Library;
4+
use DateTime;
5+
6+
function someTypeHint(array $a, DateTime $t, callable $c)
7+
{
8+
$message = '';
9+
$message .= 'Array Count: ' . count($a) . PHP_EOL;
10+
$message .= 'Date: ' . $t->format('Y-m-d') . PHP_EOL;
11+
$message .= 'Callable Return: ' . $c() . PHP_EOL;
12+
return $message;
13+
}
14+
15+
function someScalarHint(bool $b, int $i, float $f, string $s)
16+
{
17+
return sprintf("\n%20s : %5s\n%20s : %5d\n%20s " .
18+
": %5.2f\n%20s : %20s\n\n",
19+
'Boolean', ($b ? 'TRUE' : 'FALSE'),
20+
'Integer', $i,
21+
'Float', $f,
22+
'String', $s);
23+
}
24+
25+
function someBoolHint(bool $b)
26+
{
27+
return $b;
28+
}
29+

src/Chapter03/ch03_gen_ai_prompts_address_form.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ function generateNameAddressForm($fieldAttrs, $countries, $formAction = '', $for
394394
$html .= '<div class="form-group">' . PHP_EOL;
395395
$html .= '<label for="' . $fieldName . '"';
396396
$html .= ($isRequired) ? ' class="required"' : '';
397-
$html .= '>' . $fieldLabel);
397+
$html .= '>' . $fieldLabel;
398398
$html .= (!$isRequired) ? ' <span class="optional">(Optional)</span>' : '';
399399
$html .= '</label>' . PHP_EOL;
400400
// Generate input field based on type
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/*
3+
// 1st pass:
4+
function applyPercentage(array $numbers, float $percentage): array
5+
{
6+
return array_map(fn($n) => $n * ($percentage / 100), $numbers);
7+
}
8+
$arr = new ArrayObject(range(1,10));
9+
var_dump(applyPercentage($arr, 8));
10+
// result: Fatal Error
11+
*/
12+
13+
// 2nd pass:
14+
/**
15+
* Applies a percentage factor to each element in an iterable of numeric values.
16+
*
17+
* @param iterable $arr The iterable containing numeric values.
18+
* @param int $percent The percentage factor to apply (e.g., 10 for 10%, 150 for 150%).
19+
* @return iterable The modified iterable with the percentage factor applied.
20+
*/
21+
function applyPercentage(iterable $arr, int $percent): iterable
22+
{
23+
foreach ($arr as $key => $value) {
24+
yield $key => $value * ($percent / 100);
25+
}
26+
}
27+
28+
$arr = new ArrayObject(range(1,10));
29+
var_dump(iterator_to_array(applyPercentage($arr, 8)));

0 commit comments

Comments
 (0)