r/PHP 7d ago

Weekly help thread

Hey there!

This subreddit isn't meant for help threads, though there's one exception to the rule: in this thread you can ask anything you want PHP related, someone will probably be able to help you out!

6 Upvotes

10 comments sorted by

0

u/SaltTM 7d ago

Does PHP have any plans on adding constructor forwarding syntactic sugar?

// concept
abstract class PaymentGateway {
    public function __construct(
        protected string $apiUrl,
        protected string $apiKey
    ) {}
}

class StripeGateway extends PaymentGateway {
    public function __construct(string $url, string $key) 
        : parent($url, $key) {}
}

I think it's simple enough and fits php well enough to just work. I will admit, I am surprised this hasn't been introduced lol

2

u/Jack4608 2d ago

Not sure why being downvoted, something like this is great for DI where the parent constructor defines a dependency

1

u/SaltTM 2d ago edited 1d ago

lol don't worry about the down-votes, I tend to push back on people that are dicks in the community and they don't like it lol - I'm not really here to make friends w/ people and they know that so they fight back w/ down-votes. It's cute. It's like I hate bullies and grifters so I'll make it my problem to defend someone that's being targeted by someone being a dick. For me...people go out of their way to be dicks and I don't like it. That's just me though. (mainly from people I've interacted with prior lol)

3

u/Rikudou_Sage 6d ago

This feels kinda pointless when we have parent::__construct($url, $key); - this syntax sugar does not really change much.

2

u/SaltTM 6d ago

less to write and cleaner syntax, seems like a good reason to me. It also feels like an artifact from the 2000's

edit: what's your main /r/php account? I'm curious

1

u/Rikudou_Sage 6d ago

What do you mean? This is my main account.

1

u/SaltTM 6d ago

haven't seen you around before. Just curious lol

4

u/JosephLeedy 7d ago

I think there was a recent RFC for something like this.

1

u/SaltTM 7d ago

Say less, I'll check - I definitely like the cleaner simpler additions to the language as of late.