Support for Object Creation from Named Constructors in Zenstruck Foundry
Hi,
I checked the library and read through the documentation, but there is currently no way to create a new object using a named constructor. In my opinion, this should be addressed, as named constructors are very popular in DDD (Domain-Driven Design) practices today.
Thank you!
Hi @d-cichon
there is actually a feature to do this:
class MyFactory extends ObjectFactory
{
protected function initialize(): static
{
return $this
->instantiateWith(
Instantiator::namedConstructor('namedConstructorMethod')
)
}
I realize that this feature is not documented, this should be fixed
@nikophil Thank you! <3
There is problem with phpstan, but it solve my problem.
cool!
is that really a phpstan problem? or just your IDE that does not understand what's going on?
I've documented here the namedConstructor stuff.
I'll close you issue once the PR will be closed. Feel free to re-open it if something is still problematic