Is PHP 8.3 adding types to class constants?!
HTML-код
- Опубликовано: 13 сен 2024
- This is the "Typed Class Constants" 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
You forget about such an important thing as liskov subtition. Even though constants are... um, constant. However, within the inheritance chain, both the type and the value can change. The presence of a type in this case allows you not to violate such rules and, for example, Carbon::RFC3339 will be a string in the same way as base DateTimeInterface::RFC3339
P.S. Oh, I didn’t see that there was already such a comment))) It turns out to be a duplicate.
How nice is that!
I think it’s meant for inheritance too. If a superclass constant isn’t typed, you can replace it with another type, can’t you?
Good point!
@@nunomadurono actually, due to contravarian, parameter cannot have stricter type in child class, this extends to Class properties as well
I remember this well since i cannot type Model properties:
final class User extends Model{
protected string $primaryKey = 'flight_id';
}
Would be invalid. Since the parent class doesnt have type (making it mixed)
Php disallow it because it would break the contract set in the parent class