Skip to content

[draft] short constructor #19133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

[draft] short constructor #19133

wants to merge 5 commits into from

Conversation

xepozz
Copy link
Contributor

@xepozz xepozz commented Jul 15, 2025

There is PoC of short constructor syntax (inspired by Kotlin).
I'd like to discuss the possibilities to move it further and the implementation.

key points:

regular

class A;
becomes
class A {}

how to express constructor with params?

class A(private $a, public $b, $local);
becomes
class A {
    public function __construct(private $a, public $b, $local) { }
}

ok, what about inheritance?

class A(private $a, public $b, $local) extends B;
becomes
class A extends B{
    public function __construct(private $a, public $b, $local) { }
}

how to call parent constructor?

class A(private $a, public $b, $local) extends B($local, "string", 24*60*60, $this->a);
becomes
class A {
    public function __construct(private $a, public $b, $local) {
        parent::__construct($local, "string", 24*60*60, $this->a);
    }
}

@iluuu1994 iluuu1994 added the RFC label Jul 15, 2025
@iluuu1994
Copy link
Member

Variations of this have discussed during the constructor property promotion RFC, and more recently in the records RFC.1 As with previous changes, this needs a discussion on the internals mailing list. My expectation is that you'll likely face significant resistance to this change.

Footnotes

  1. https://wiki.php.net/rfc/records

@xepozz
Copy link
Contributor Author

xepozz commented Jul 15, 2025

@iluuu1994 thanks for the answer. Records RFC pushed me to implement such feature for classes as well, making "records" look similar to classes to prove that it could be done "with classes"
Btw, it's just a part of process of simplification creating classes

class UUID(private readonly string $value);

This is what you need to create a data transfer object

Btw, I'd like to discuss implementation. Theoretically, will it be enough to push the RFC further?
Now the "short constructor" is just a sugarred version of long constructor. Tests with AST printer prove it.

@iluuu1994
Copy link
Member

Btw, I'd like to discuss implementation.

Generally, I don't spend too much time on implementation before knowing that the RFC actually progresses. Most RFCs die before they make it to the vote.

Theoretically, will it be enough to push the RFC further?

Yes, RFCs are the only way to land language changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants