Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个Parent具有 2 个子实体 (Foo和Bar) 实现SINGLE_TABLE继承的实体。
Parent
Foo
Bar
SINGLE_TABLE
是否可以创建一个new Parent()实体并动态设置它的鉴别器foo而不是创建一个new Foo()?
new Parent()
foo
new Foo()
不,没有,如果您真的需要评论中提到的场景,那么使用某种工厂方法可能会更好:
abstract class MyParent { public static function fromString($type) { switch ($type) { case 'foo': return new Foo(); case 'bar': return new Bar(); } throw new DomainException('Unknown type: ' . $type); } }