public function sum(): int
{
return 10 + "5e2";
}
Typing error
This is a num (int/float) because
this is used in an arithmetic
operation.
It is incompatible with a string.
Slide 44
Slide 44 text
ܕม: mixed
Slide 45
Slide 45 text
public function get($id): mixed
{
return //Կ͔Λฦ٫͢Δ;
}
$container = new Container();
$container->get('something');
Slide 46
Slide 46 text
private function invariantLoggerInterface(
Container $container,
): LoggerInterface {
$logger = $container
->get(LoggerInterface::class);
invariant(
$logger instanceof LoggerInterface,
"Interface '\Psr\Log\LoggerInterface' is not
implemented by this class",
);
return $logger;
}
mixedͷ߹Կ͕ฦ٫͞ΕΔ͔Θ͔Βͳ͍
ظͷͷ͕ฦ٫͞ΕΔ͔Ͳ͏͔
ඞͣهड़ͯ͠ɺTypeChecker͚ʹهड़
Slide 47
Slide 47 text
final class Util {
public function something(mixed $any): mixed {
if($any is int) {
//
}
if($any is string) {
//
}
}
}
Slide 48
Slide 48 text
Type Constants
Slide 49
Slide 49 text
interface TypeInterface {
abstract const type T;
public function getNative(): this::T;
}
Slide 50
Slide 50 text
class UserType implements TypeInterface {
const type T = Vector;
public function getNative(): this::T {
return new Vector([1,2]);
}
}
Slide 51
Slide 51 text
Factory
Slide 52
Slide 52 text
class Sample
{
}
Slide 53
Slide 53 text
final class Factory
{
protected array $array = [
'Sample' => Sample::class
];
public function get(string $id)
{
$key = \ucfirst(\strtolower($id));
if(\array_key_exists($key, $this->array)) {
$class = $this->array[$key];
return new $class();
}
}
}
class_existsͳͲ
Slide 54
Slide 54 text
<<__ConsistentConstruct>>
class Sample {
}
constructor੍ޚ
Slide 55
Slide 55 text
final class Factory {
protected Map> $map = Map{
'Sample' => Sample::class
};
public function get(string $id): Sample {
$class = $this->map->get(\ucfirst(\strtolower($id)));
if (!\is_null($class)) {
return new $class();
}
throw new \RuntimeException();
}
}
classจࣈྻࢦఆ
࣮֬ͳΠϯελϯεੜ
Slide 56
Slide 56 text
ࣝผͷҧ͍
Slide 57
Slide 57 text
final class BookId
{
private $id;
public function __construct(string $id)
{
$this->id = $id;
}
public function getValue(): string
{
return $this->id;
}
}
Slide 58
Slide 58 text
final class BookId {
public function __construct(
private string $id
) {}
public function getValue(): string {
return $this->id;
}
}
Slide 59
Slide 59 text
ҧ͏දݱํ๏
Slide 60
Slide 60 text
abstract class Identifier {
public function __construct(
protected T $id
) {}
public function getValue(): T {
return $this->id;
}
}
final class BookId extends Identifier {
}
Generics
Slide 61
Slide 61 text
ॻ੶දݱํ๏
Slide 62
Slide 62 text
class Book
{
private $id;
private $title;
private $price;
public function __construct(
BookId $id,
BookTitle $title,
Price $price
) {
$this->id = $id;
$this->title = $title;
$this->price = $price;
}
public function getId(): BookId {
return $this->id;
}
// লུ
}
Slide 63
Slide 63 text
class Book {
public function __construct(
private BookId $id,
private BookTitle $title,
private Price $price
) {}
public function getId(): BookId {
return $this->id;
}
// লུ
}
ݎ੍͍
ݎ੍͍
class BookCollection
{
protected $books = [];
public function __construct(array $books = [])
{
$this->books = $books;
}
public function toArray(): array {
$books = [];
foreach($this->books as $book) {
$books[] = new Book(
new BookId($book['id']),
new BookTitle($book['title']),
new Price($book['price'])
);
}
return $books;
}
}
ϑΟʔϧυ੍ͳ͠
Slide 67
Slide 67 text
type BookShape = shape(
'book_id' => string,
'title' => string,
'price' => int
);
ϑΟʔϧυ੍
Slide 68
Slide 68 text
class BookCollection {
protected Vector $v = Vector{ };
public function __construct(
protected varray $array
) {
$this->v = $this->vec();
}
protected function vec(): Vector {
$v = Vector{ };
foreach($this->array as $row) {
$v->add(new Book(
new BookId($row['book_id']),
new BookTitle($row['title']),
new Price($row['price'])
));
}
return $v;
}
public function toArray(): varray {
return $this->v->toVArray();
}
}
shapeΛvarrayͰ
to Vector
Slide 69
Slide 69 text
$v = new Vector([
new Book('1234', 'testing', 2999),
new Book('1235', 'testing', 2999),
]);
$v = $v->filter(
($t) ==> $t->getId() === '1234'
)->immutable();
collection filter
Πϛϡʔλϒϧʹ