[PHP-DEV] Fully overridable factory methods

Hi,

A recent discussion brought up the need for multiple constructors for the same class.

https://externals.io/message/126428

The need was mostly answered by suggestions to use factory methods.

However on an earlier discussion it was discussed how it’s fine and even useful for constructor methods to violate the LSP.

https://externals.io/message/121789

I’ve felt a similar need on some projects. To have a named factory method that would have an overridable signature.

Maybe a new keyword like factorythat would make the method only callable statically? I’m thinking of something like

class Triangle extends Polygon

{

public factory method fromPoints(Point $p1, Point $p2, Point $p3): static
{

// …

}

public factory method wrap(static|string $triangle): static
{

return $triangle instanceof static ? $triangle : static::fromWkt($triangle);

}

}

Sure, I’m not saying this API is currently impossible. I can do the more narrow checks within the methods and throw from there. But sometimes it’s useful to precisely describe the acceptable arguments in the method signature.

What do you think? Would such a feature be useless and/or harmful?

BR,

Juris

On Tue, Feb 18, 2025, at 17:52, Juris Evertovskis wrote:

Hi,

A recent discussion brought up the need for multiple constructors for the same class.

https://externals.io/message/126428

The need was mostly answered by suggestions to use factory methods.

However on an earlier discussion it was discussed how it’s fine and even useful for constructor methods to violate the LSP.

https://externals.io/message/121789

I’ve felt a similar need on some projects. To have a named factory method that would have an overridable signature.

Maybe a new keyword like factorythat would make the method only callable statically? I’m thinking of something like

class Triangle extends Polygon

{

public factory method fromPoints(Point $p1, Point $p2, Point $p3): static

{

// …

}

public factory method wrap(static|string $triangle): static

{

return $triangle instanceof static ? $triangle : static::fromWkt($triangle);

}

}

Sure, I’m not saying this API is currently impossible. I can do the more narrow checks within the methods and throw from there. But sometimes it’s useful to precisely describe the acceptable arguments in the method signature.

What do you think? Would such a feature be useless and/or harmful?

BR,

Juris

Hey Juris:

You may be interested in a super-draft RFC that I’ll propose in the next few days… (just fixing a couple of edge cases in the implementation) it allows for nested classes and can support what you are thinking. I will add this use case to the RFC.

Adapting your example:

class Triangle extends Polygon {

public final class fromPoints extends Triangle {

public function __construct(Point $p1, Point $p2, Point $p3) {}

}

public final class wrap extends Triangle {

public function __construct(Triangle $triangle) {}

}

}

$triange = new Triangle::fromPoints(…$points);

I don’t know if this is what you are thinking of, but it basically allows the expression you are suggesting. If we were to implement your idea though, then nested classes would probably be impossible (and vice-versa).

— Rob

Hi

On 2/18/25 18:31, Rob Landers wrote:

You may be interested in a super-draft RFC that I’ll propose in the next few days…

May I kindly request some breathing room with regard to new RFC discussions so that folks are able to give each RFC the proper attention. The list currently is super active. We have:

- 2 Votes in progress (FCC in constexpr, aviz for static)
- 6 discussions with RFC written (NoDiscard, get_error_handler, URI, Compression, Optional Interfaces, Pipe)
- and several non-RFC or pre-RFC discussions (Async, Constructor Overloading, Not Null Assertions, …)

This is already approximately 3 RFC discussions too many. Perhaps wait until Mid-March or so? I expect at least some of the currently discussed RFCs to be in voting by then.

Best regards
Tim Düsterhus

On 2025-02-18 19:31, Rob Landers wrote:

On Tue, Feb 18, 2025, at 17:52, Juris Evertovskis wrote:

Hi,

A recent discussion brought up the need for multiple constructors for the same class.

https://externals.io/message/126428

The need was mostly answered by suggestions to use factory methods.

However on an earlier discussion it was discussed how it’s fine and even useful for constructor methods to violate the LSP.

https://externals.io/message/121789

I’ve felt a similar need on some projects. To have a named factory method that would have an overridable signature.

Maybe a new keyword like factorythat would make the method only callable statically? I’m thinking of something like

class Triangle extends Polygon

{

public factory method fromPoints(Point $p1, Point $p2, Point $p3): static
{

// …

}

public factory method wrap(static|string $triangle): static
{

return $triangle instanceof static ? $triangle : static::fromWkt($triangle);

}

}

Sure, I’m not saying this API is currently impossible. I can do the more narrow checks within the methods and throw from there. But sometimes it’s useful to precisely describe the acceptable arguments in the method signature.

What do you think? Would such a feature be useless and/or harmful?

BR,

Juris

Hey Juris:

You may be interested in a super-draft RFC that I’ll propose in the next few days… (just fixing a couple of edge cases in the implementation) it allows for nested classes and can support what you are thinking. I will add this use case to the RFC.

Adapting your example:

class Triangle extends Polygon {
public final class fromPoints extends Triangle {
public function __construct(Point $p1, Point $p2, Point $p3) {}
}

public final class wrap extends Triangle {
public function __construct(Triangle $triangle) {}
}
}

$triange = new Triangle::fromPoints(…$points);

I don’t know if this is what you are thinking of, but it basically allows the expression you are suggesting. If we were to implement your idea though, then nested classes would probably be impossible (and vice-versa).

— Rob

Hi Rob,

I’m a bit confused about the use of final in your example.

Would it allow defining Polygon::fromPoints such that it accepts variable number of arguments and Triangle::fromPoints such that it only accepts exactly 3 args?

Or having a hierarchy like Square extends Rectangle extends Polygon while having a wrap with the following covariant behaviour?

  • Polygon::wrap(Polygon $poly) – accepts any Polygons
  • Rectangle::wrap(Rectangle $poly) – accepts Rectangles and Squares, but not other Polygons
  • Square::wrap(Square $poly) – only accepts Square arguments

Juris

On Tue, Feb 18, 2025, at 20:24, Tim Düsterhus wrote:

Hi

On 2/18/25 18:31, Rob Landers wrote:

You may be interested in a super-draft RFC that I’ll propose in the next few days…

May I kindly request some breathing room with regard to new RFC

discussions so that folks are able to give each RFC the proper

attention. The list currently is super active. We have:

  • 2 Votes in progress (FCC in constexpr, aviz for static)

  • 6 discussions with RFC written (NoDiscard, get_error_handler, URI,

Compression, Optional Interfaces, Pipe)

  • and several non-RFC or pre-RFC discussions (Async, Constructor

Overloading, Not Null Assertions, …)

This is already approximately 3 RFC discussions too many. Perhaps wait

until Mid-March or so? I expect at least some of the currently discussed

RFCs to be in voting by then.

Best regards

Tim Düsterhus

Hi Tim,

I assume you are speaking to me, personally?

This will be opened as a draft before proposing it to the list, which will indeed (probably) be around mid-March, pending solicited feedback, so no worries. I also have a couple drafts/in-discussion to close that this new RFC will supersede.

— Rob

On Tue, Feb 18, 2025, at 20:29, Juris Evertovskis wrote:

On 2025-02-18 19:31, Rob Landers wrote:

On Tue, Feb 18, 2025, at 17:52, Juris Evertovskis wrote:

Hi,

A recent discussion brought up the need for multiple constructors for the same class.

https://externals.io/message/126428

The need was mostly answered by suggestions to use factory methods.

However on an earlier discussion it was discussed how it’s fine and even useful for constructor methods to violate the LSP.

https://externals.io/message/121789

I’ve felt a similar need on some projects. To have a named factory method that would have an overridable signature.

Maybe a new keyword like factorythat would make the method only callable statically? I’m thinking of something like

class Triangle extends Polygon

{

public factory method fromPoints(Point $p1, Point $p2, Point $p3): static

{

// …

}

public factory method wrap(static|string $triangle): static

{

return $triangle instanceof static ? $triangle : static::fromWkt($triangle);

}

}

Sure, I’m not saying this API is currently impossible. I can do the more narrow checks within the methods and throw from there. But sometimes it’s useful to precisely describe the acceptable arguments in the method signature.

What do you think? Would such a feature be useless and/or harmful?

BR,

Juris

Hey Juris:

You may be interested in a super-draft RFC that I’ll propose in the next few days… (just fixing a couple of edge cases in the implementation) it allows for nested classes and can support what you are thinking. I will add this use case to the RFC.

Adapting your example:

class Triangle extends Polygon {

public final class fromPoints extends Triangle {

public function __construct(Point $p1, Point $p2, Point $p3) {}

}

public final class wrap extends Triangle {

public function __construct(Triangle $triangle) {}

}

}

$triange = new Triangle::fromPoints(…$points);

I don’t know if this is what you are thinking of, but it basically allows the expression you are suggesting. If we were to implement your idea though, then nested classes would probably be impossible (and vice-versa).

— Rob

Hi Rob,

I’m a bit confused about the use of final in your example.

The gist is that visibility applies to the outer class while final/readonly/etc apply to the inner class. So, in this case it is public (can instantiate it from outside the class) and final (cannot extend it, which would be weird to extend an inner class. I’m still debating if I want to make inner classes final by default – but that will probably come up in the actual rfc discussion).

There’s already a thread discussing this topic from a few weeks ago, so I don’t mind discussing some details and “testing the waters”, as they say.

Would it allow defining Polygon::fromPoints such that it accepts variable number of arguments and Triangle::fromPoints such that it only accepts exactly 3 args?

Or having a hierarchy like Square extends Rectangle extends Polygon while having a wrap with the following covariant behaviour?

  • Polygon::wrap(Polygon $poly) – accepts any Polygons

  • Rectangle::wrap(Rectangle $poly) – accepts Rectangles and Squares, but not other Polygons

  • Square::wrap(Square $poly) – only accepts Square arguments

Juris

The inner class is its own class and belongs to the outer class – it is inherited similar to a static property. So to answer your question, yes, all of that is possible. From inside the outer class you can even use static:: or self:: or parent:: to access the inner classes as well, so it should be quite flexible for these types of hierarchies and allow for some nice generic code reuse (e.g. parent::wrap($this))

— Rob