Aktywne Wpisy
whitepiranha +50
Czym jest ten dziwny obiekt na niebie lecąy na zachod? #podkarpackie
trike +27
Chłop z mojej wiochy 24 lata morda chad, wysoki, piękna kobieta, miał szkółkę pływacka i popełnił samobója ehhh ja mając takie życie i wygląd jak on nigdy bym nawet o tym nie pomyślał. #przegryw #samobojstwo
#php
3 lata temu pojawiła się już propozycja RFC > https://wiki.php.net/rfc/property_accessors (sekcja Properties in interfaces), ale raczej nie prędko to ujrzymy (jeśli w ogóle).
<?php
abstract class a{
}
class b extends a{
}
class c {
public function __construct(public a $var){}
}
class d extends c {
}
$d = new d(new b());
interface UserInterface {
// Interface requires that property is public readable.
public readonly string $name;
}
class User implements UserInterface {
// Implemented property
public readonly string $name = 'dsadasd';
// or via dependency constructor
public function __construct(public readonly string $name) {}
}
Dzięki temu możesz wstrzyknąć Usera pod UserInterface i mieć pewność, że obiekt zawiera propa.
Jak nie było "readonly" w starych wersjach PHPa,
BTW w Javie: Variables which declared in interface are by default public, static