#[Override] attribute is coming to PHP 8.3
HTML-код
- Опубликовано: 13 сен 2024
- This is the "#[Override] attribute" segment from my "What's new in PHP 8.3 " livestream. Watch the full livestream here: www.youtube.co....
----------------------------------------------------------------------------------------------------
- Twitter: / enunomaduro
- Telegram Group: t.me/+RoAqqaaS...
- Threads: www.threads.ne...
- Mastodon: mastodon.socia...
- LinkedIn: / nunomaduro
- RUclips: / nunomaduro
- GitHub: github.com/nun...
- Bluesky: bsky.app/profi...
- Tiktok: / enunomaduro
estou a usar a tua library openAI PHP e vim aqui dar follow. é bom ver portugal a representar.
Siiiiuuuu!
Very cool, although we should strive towards not using inheritance
This may be a stupid question, so beware :)
In your code, the returned value is 'foo', from the parent class method - even when the override attribute is used. Basically the foo() from the child class isn't used at all, just to check if the method exists in its parent.
So why go to all those lengths with an #Override attribute? Just to ensure the method exists in the parent class?
The var_dump method is been used in a Person class, not an AnotherPerson class.
That's why it shows foo when he runned in terminal.
@@GabrielPHahn oh yes, you're right... Thanks! It makes sense now 🙂
imho keyword would be better, just like PHP already have readonly, that does the same as readonly in C#
Thanks for the amazing tutorials. Can you make a video about sublime setup for php? thanks
Great suggestion!
Poeple are moving on with Composition and PHP is still making icing for Inheritance.
This may be a noobie syndrome but I don't get it 🤷
Like why would someone need this ?
Explanations are welcomed 🤗
Well, this attribute is used to indicate that a method in a subclass is intended to override a method in its parent class. It helps to ensure that you are actually overriding a method as you intend, and it provides better code readability.
RFC: wiki.php.net/rfc/marking_overriden_methods.
PHP is slowly moving to a more strict and type safe version of itself.
In the example, the parent class provides something like an interface that child classes can either inherit or override. You may decide to rework the interface and remove the method from all classes. However, it's easy to miss a child class that has the method and have it malfunction silently. This annotation makes the intent of the programmer much more clear and provides a way for LSPs like IntelliPhense, PHPactor, PHPstan and so on to perform static analysis and give you squiggle lines in the editor or warnings during CI.
Similar like java 😮
Exactly!