2 回答
TA贡献2041条经验 获得超4个赞
简短的回答是“否”,因为 PHP 中缺少泛型类。
要解决此问题,您应该ValueObjects在实现类中继续使用抽象类型(此处为),然后自己检查实际类型。
class UserCollection implements Collection {
public function add( ValueObjects $obj ) : bool
{
if (!($obj instanceof User)) {
throw new RuntimeException('...');
}
/** @var User $obj */
// The above comment make your IDE know the $obj is a `User` instance
// your actual logic
return true;
}
}
一个小提示,您不需要在 PHP中将$obj对象从ValueObjectsto 转换User。@var上面代码中的phpDoc 内联注释行仅告诉 IDE 这$obj是一个User实例并支持User方法的自动完成。没有它,PHP 脚本仍然运行。
TA贡献1752条经验 获得超4个赞
我会按照这些思路争论一些事情。请注意我如何扩展客观收集的核心概念,同时作为一个自我验证的主题。然后我构成这些各种构建成可知,混凝土组合。
interface Transportable {};
interface User {};
interface Collected
{
public function isValid($item): bool;
// Contract, add is deterministic, require no return
public function add(Transportable $item): void;
}
trait SanityChecked
{
public function isValid($item): bool
{
return true;
}
}
abstract class Collector implements Collected
{
use SanityChecked;
private $items = [];
public function add(Transportable $item): void
{
if ($this->isValid($item) && $this->items[] = $item) {
return;
}
throw new Exception('This is the not the droid we were looking for.');
}
}
class Users extends Collector
{
public function isValid($test): bool
{
return $test instanceof User;
}
}
这可以被嘲笑为:
$users = new Users();
$users->add(new class() implements Transportable, User {});
echo 'Added first!'.PHP_EOL;
$users->add(new class() implements User {}); // Sorry, error.
echo 'Added second!'.PHP_EOL;
https://3v4l.org/O2qfJ
另一种看待它的方式是进一步扩展 trait 的行为:
trait ValidatesAsUser
{
public function isValid(Transportable $user): bool
{
return $user instanceof User;
}
}
class PortalUsers extends Collector
{
use ValidatesAsUser;
}
class ContentEditors extends PortalUsers {}
class Authors extends ContentEditors {}
class AuthorsWithPublishedStoriesByRating extends Authors {}
我认为关于预测的最后一部分特别有趣。
- 2 回答
- 0 关注
- 150 浏览
添加回答
举报