Re: [PHP-DEV] Typed constants revisited
On Fri, Mar 25, 2022 at 10:55 AM Guilliam Xavier wrote: > I intentionally left `abstract` out of `public const bool CAN_FLY;` in the >> `abstract class` for consistency with the implementation with `interface`, >> which would also have to be `public const bool CAN_FLY;`. Currently >> `abstract` is only used in front of methods `abstract function doThing(): >> bool;`. Open to discussion - which way is ideal or preferred? That could be >> included as a subset of an RFC vote if a consensus during discussion isn't >> reached. >> > > I understand, but note that methods are implicitly abstract in an > interface, but it must be explicit in an abstract class; and since I see > the proposed feature mainly as a "replacement" for abstract static methods > [whose all implementations just return a literal value]... (anyway, not > super important) > Constants are not abstract in an interface - they must be assigned a value. Only classes and methods can be abstract. Within an abstract class it is not valid to have an abstract property. Properties can be defined `protected int $id;` and optionally assigned a value `protected int $id = 5;`, but cannot be `abstract protected int $id;`. So to me it makes more sense to have constants follow the same syntax as properties `public const bool CAN_FLY;` without the `abstract` keyword. An example: ``` abstract class Bird { public const bool CAN_FLY; protected bool $isExtinct; ``` This allows for similar behavior, similar requirements, and similar syntax - consistency ftw! There seems to be interest and good use cases (thanks Sara for the good practical example!). At this point I'm going to work on a new RFC with all the details and feedback from this discussion.
Re: [PHP-DEV] Requesting wiki, RFC karma
On 25.03.2022 at 18:01, Mark Niebergall wrote: > I would like to request wiki edit privileges and RFC karma. > > Quick intro - I co-organize the Utah PHP Usergoup, speak at PHP and > tech conferences, volunteer for cyber security exam certification > development, work on a vulnerability management platform with PHP, and have > been developing in PHP for ~17 years. > > Plans - I'm picking up the typed constants RFC where it was left off, > planning to create a new RFC for this go around. I have a few other future > ideas I've also been tinkering with and plan to create several other RFCs > in the future. > > Username: mbniebergall RFC karma granted. Best of luck with the RFC. :) -- Christoph M. Becker -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
[PHP-DEV] Requesting wiki, RFC karma
Hello All, I would like to request wiki edit privileges and RFC karma. Quick intro - I co-organize the Utah PHP Usergoup, speak at PHP and tech conferences, volunteer for cyber security exam certification development, work on a vulnerability management platform with PHP, and have been developing in PHP for ~17 years. Plans - I'm picking up the typed constants RFC where it was left off, planning to create a new RFC for this go around. I have a few other future ideas I've also been tinkering with and plan to create several other RFCs in the future. Username: mbniebergall Thanks, Mark Niebergall
Re: [PHP-DEV] Typed constants revisited
> I intentionally left `abstract` out of `public const bool CAN_FLY;` in the > `abstract class` for consistency with the implementation with `interface`, > which would also have to be `public const bool CAN_FLY;`. Currently > `abstract` is only used in front of methods `abstract function doThing(): > bool;`. Open to discussion - which way is ideal or preferred? That could be > included as a subset of an RFC vote if a consensus during discussion isn't > reached. > I understand, but note that methods are implicitly abstract in an interface, but it must be explicit in an abstract class; and since I see the proposed feature mainly as a "replacement" for abstract static methods [whose all implementations just return a literal value]... (anyway, not super important)
Re: [PHP-DEV] Typed constants revisited
Guilliam, On Fri, Mar 25, 2022 at 6:35 AM Guilliam Xavier wrote: > Hi Mark, > > On Wed, Mar 23, 2022 at 11:55 PM Mark Niebergall > wrote: > >> (...) >> >> Another example I often see in my projects could be used with a similar >> example: >> >> ``` >> abstract class Bird >> { >> public const bool CAN_FLY; >> public const string FAMILY; >> public function canFly(): bool >> { >> return self::CAN_FLY; >> } >> } >> final class EmperorPenguin extends Bird >> { >> public const bool CAN_FLY = false; >> public const string FAMILY = 'penguin'; >> } >> ``` >> > > I had this "need" too (and used abstract static methods where the > implementations just return a literal value...). > > Just 2 remarks: in abstract class Bird, shouldn't it be: > - "*abstract* public const bool CAN_FLY;" (and same for FAMILY) > - "return *static*::CAN_FLY;" > ? > I intentionally left `abstract` out of `public const bool CAN_FLY;` in the `abstract class` for consistency with the implementation with `interface`, which would also have to be `public const bool CAN_FLY;`. Currently `abstract` is only used in front of methods `abstract function doThing(): bool;`. Open to discussion - which way is ideal or preferred? That could be included as a subset of an RFC vote if a consensus during discussion isn't reached. Good correction, yes, `return static::CAN_FLY;` in that example in the concrete child class. > > Regards, > > -- > Guilliam Xavier >
Re: [PHP-DEV] [RFC][Under discussion] Arbitrary string interpolation
Hey Ilija. On 25.03.22 15:38, Arnaud Le Blanc wrote: Hi Ilija I find that sprintf() is easier to read in most cases. One reason for this is that the text is separated from the code. It's also easier to review for humans and linters. The strtoupper example would look like this with sprintf: $world = 'world'; echo sprintf('Hello %s!', strtoupper($world)); Longer examples can be nicely split in multiple lines: echo sprintf( 'Received HTTP status code %d (reason phrase: %s)', $response->getStatusCode(), $response->getReasonPhrase(), ); This technique also allows me to use the original string in translation while still keeping the replacements out of that. So something like echo sprintf( gettext('Hello %s!'), strtoupper($world) ); would easily work and provide the translator only with the string `Hello %s` which is much less to break than `Hello {strtoupper($world)}!` So for internationalized applications I'd rather keep the sprintf approach than a new string-syntax. A way to use sprintf in a more out-of-the-box way like in ruby would be awesome! Something like echo 'Hello %s' % [strtoupper($world)]; or echo 'Hello %{world}' % ['world' => strtoupper($world)]; But I'm absolutely happy with the current sprintf functionality. Just my 0.02€ Cheers Andreas -- ,,, (o o) +-ooO-(_)-Ooo-+ | Andreas Heigl | | mailto:andr...@heigl.org N 50°22'59.5" E 08°23'58" | | https://andreas.heigl.org | +-+ | https://hei.gl/appointmentwithandreas | +-+ OpenPGP_0xA8D5437ECE724FE5.asc Description: OpenPGP public key OpenPGP_signature Description: OpenPGP digital signature
Re: [PHP-DEV] [RFC][Under discussion] Arbitrary string interpolation
Hi Ilija I find that sprintf() is easier to read in most cases. One reason for this is that the text is separated from the code. It's also easier to review for humans and linters. The strtoupper example would look like this with sprintf: $world = 'world'; echo sprintf('Hello %s!', strtoupper($world)); Longer examples can be nicely split in multiple lines: echo sprintf( 'Received HTTP status code %d (reason phrase: %s)', $response->getStatusCode(), $response->getReasonPhrase(), ); And this also works with heredoc: echo sprintf( <<<'HTML' %s HTML, htmlspecialchars($title), ); -- Arnaud On jeudi 17 mars 2022 23:27:30 CET Ilija Tovilo wrote: > Hi everyone > > I'd like to start discussion on a new RFC for arbitrary string > interpolation. https://wiki.php.net/rfc/arbitrary_string_interpolation > > Let me know what you think. > > Ilija -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] Typed constants revisited
Hi Mark, On Wed, Mar 23, 2022 at 11:55 PM Mark Niebergall wrote: > (...) > > Another example I often see in my projects could be used with a similar > example: > > ``` > abstract class Bird > { > public const bool CAN_FLY; > public const string FAMILY; > public function canFly(): bool > { > return self::CAN_FLY; > } > } > final class EmperorPenguin extends Bird > { > public const bool CAN_FLY = false; > public const string FAMILY = 'penguin'; > } > ``` > I had this "need" too (and used abstract static methods where the implementations just return a literal value...). Just 2 remarks: in abstract class Bird, shouldn't it be: - "*abstract* public const bool CAN_FLY;" (and same for FAMILY) - "return *static*::CAN_FLY;" ? Regards, -- Guilliam Xavier