foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Support for Object Creation from Named Constructors in Zenstruck Foundry

Open d-cichon opened this issue 1 year ago • 4 comments

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!

d-cichon avatar Oct 16 '24 14:10 d-cichon

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 avatar Oct 17 '24 08:10 nikophil

@nikophil Thank you! <3

There is problem with phpstan, but it solve my problem.

image

d-cichon avatar Oct 17 '24 09:10 d-cichon

cool!

is that really a phpstan problem? or just your IDE that does not understand what's going on?

nikophil avatar Oct 17 '24 09:10 nikophil

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

nikophil avatar Oct 18 '24 13:10 nikophil