SA
Size: a a a
SA
AB
SA
AB
SA
AB
AB
AB
AB
SA
<?php
interface ResizableInterface {
public function width(): int;
public function setWidth(int $arg);
}
interface NamedInterface {
public function name(): string;
}
class WidgetClass implements NamedInterface, ResizableInterface {
private $name;
private $width;
public function __construct(string $name, int $width) {
$this->name = $name;
$this->width = $width;
}
public function name(): string {
return $this->name;
}
public function width(): int {
return $this->width;
}
public function setWidth(int $arg) {
$this->width = $arg;
}
}
class FactoryClass {
public function generate(): NamedInterface {
$widget = new WidgetClass('top', 42);
return $widget;
}
}
$f = new FactoryClass();
// хотелось бы чтобы в $w были только методы
// NamedInterface доступны
$w = $f->generate();
// а по факту доступно всё, что в WidgetClass.
// Можно ли заставить генерировать тут ошибку?
$w->setWidth(42);
SA
AB
<?php
interface ResizableInterface {
public function width(): int;
public function setWidth(int $arg);
}
interface NamedInterface {
public function name(): string;
}
class WidgetClass implements NamedInterface, ResizableInterface {
private $name;
private $width;
public function __construct(string $name, int $width) {
$this->name = $name;
$this->width = $width;
}
public function name(): string {
return $this->name;
}
public function width(): int {
return $this->width;
}
public function setWidth(int $arg) {
$this->width = $arg;
}
}
class FactoryClass {
public function generate(): NamedInterface {
$widget = new WidgetClass('top', 42);
return $widget;
}
}
$f = new FactoryClass();
// хотелось бы чтобы в $w были только методы
// NamedInterface доступны
$w = $f->generate();
// а по факту доступно всё, что в WidgetClass.
// Можно ли заставить генерировать тут ошибку?
$w->setWidth(42);
SA
AB
AB
AB
AB
AB
SA