Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119335 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 8988 invoked from network); 18 Jan 2023 20:27:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jan 2023 20:27:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CFDC618050B for ; Wed, 18 Jan 2023 12:27:58 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,FREEMAIL_REPLYTO_END_DIGIT,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 18 Jan 2023 12:27:58 -0800 (PST) Received: by mail-pf1-f180.google.com with SMTP id x4so23021270pfj.1 for ; Wed, 18 Jan 2023 12:27:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=GPg9fJhh5DEvDnkJ12claCIAzKtHZmqK4XtnOz7GR8s=; b=m2OowHnrUOstvlgEfJyTrZn55EYi7Ufy/BHaffHAfrNtatBHYsAkeLZlIKd8hGoy6t Pftux6UwJkzOhtaFb/ZpRwFOQ0OIRJ39mdf+EnqfoqN/4s3YX3GezIwjuagujiOTNG4H euyYXjB/Zh8bF/+C6IMyFFOUMmfYn40TCOpYOJhrGMwdCIuJFEhvv6yRlCncHVE7ZjcY uyKudp5GGxBXitQzzd4/Yq3deHQeqDeqq2hAUhJzevj72GIjoNQ9pFr5L0qxICe8BacZ /9bdjyEJMLTksSnMt+krqWAZfcBUJNmKRABQQeuGK71/xhNlWulzQ7o6PWYfhSSS4czh 7R1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GPg9fJhh5DEvDnkJ12claCIAzKtHZmqK4XtnOz7GR8s=; b=VkZi4ptpzP+X6X1MtjJcIFG2qKTqVwvY+f4/3Z1gEcapr5uo5qKoh+GkVULAfEHLIJ zMcS4oDvaS/WraVFbXX28zCqQdRu4xYhoUc6+zsOjVF20+xw79lbVNT5/0fboFmFpmyA lAGeVv7dBkf4yeVGvNk/ANoZR5cmN/yVz+buJZS4EKh6NUxVP83W+AfdFByQjo+zIQ+Q 9rzZ6dSj7/DdEjmABnBffijFHlEQzklwrUUymhqtcxLawxFjB7EmO1uNzNpuIRRr9Rqt J9PMkG1LtcayS6o+Pgh+zwhnhWXynWrSg9szEKRH2UAWhy0dD5Vj1F4XMUgSjH3gbPa3 EsNw== X-Gm-Message-State: AFqh2krrPrhU4P22becJz+HtV3tomxYipPUz540w4Pi7neiYT5K31zM+ eGd5caWLZDiY+x7ut8JGzcyKnK1rbPkp9ICpblltH6xqVhyMRQ== X-Google-Smtp-Source: AMrXdXt4wnf+eV48GnjFWj5kYFm/7QR1mtggXY420LmAs1ZmYRT2rxPgmC0gAHc+3JRcx5/2nnSccI/nItCdmFTF5Vk= X-Received: by 2002:a05:6a00:2992:b0:58d:b26a:6238 with SMTP id cj18-20020a056a00299200b0058db26a6238mr858096pfb.84.1674073677496; Wed, 18 Jan 2023 12:27:57 -0800 (PST) MIME-Version: 1.0 References: <04288995-54CE-41A4-BFFD-A7CF72F16D3F@gmail.com> In-Reply-To: Reply-To: autaut03@gmail.com Date: Wed, 18 Jan 2023 22:27:44 +0200 Message-ID: To: Claude Pache Cc: Kamil Tekiela , PHP Developers Mailing List Content-Type: multipart/alternative; boundary="000000000000e0050305f28fa68c" Subject: Re: [PHP-DEV] [RFC] Path to Saner Increment/Decrement operators From: autaut03@gmail.com (Alex Wells) --000000000000e0050305f28fa68c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jan 18, 2023 at 10:09 PM Claude Pache wrote: > > > Le 18 janv. 2023 =C3=A0 19:33, Alex Wells a =C3=A9cr= it : > > Classes and methods is the expected way of implementing standard library > in an OO language. New APIs (such as the new Random api) use OO instead o= f > functions and it makes more sense to use OO in this case too: there's > likely a place for other methods too, like toBase(int $otherBase) etc. It > would also be possible to use overloaded operators if needed. > > > Fortunately, PHP is not (yet) a language where every problem requires the > use and manipulation of objects implementing a generalised and unified > solution. I guess that the OO way of writing: > > ```php > function next_alpha_id(): string { > static $x =3D 'zz'; > return ++$x; > } > > function next_num_id(): int { > static $x =3D 0; > return ++$x; > } > > $my_id =3D next_alpha_id(); > $my_other_id =3D next_num_id(); > ``` > > would resemble to the following, except that `mixed` should be replaced b= y > the use of generics. For brevity, I left the mandatory interfaces as > exercise to the reader. > > ```php > class IdGenerator { > > protected mixed $current; > > public function __construct( > protected readonly IdGeneratorType $type > , protected readonly IdGeneratorDirection $direction > , mixed $start > ) { > $this->current =3D $start; > } > > public function next(): mixed { > // implementation... > } > > } > > enum IdGeneratorType { > case alphabetic; > case numeric; > } > > enum IdGeneratorDirection { > case positive; > case negative; > } > > > > final class StandardGlobalAlphabeticIdGenerator { > > private static IdGenerator $id_generator; > > public static function get(): IdGenerator { > return self::$id_generator ?? new IdGenerator( > type: IdGeneratorType::alphabetic > , direction: IdGeneratorDirection::positive > , start: 'aaa' > ); > } > > } > > final class StandardGlobalNumericIdGenerator { > > private static IdGenerator $id_generator; > > public static function get(): IdGenerator { > return self::$id_generator ?? new IdGenerator( > type: IdGeneratorType::numeric > , direction: IdGeneratorDirection::positive > , start: 1 > ); > } > > } > > > $my_id =3D StandardGlobalAlphabeticIdGenerator::get()->next(); > $my_other_id =3D StandardGlobalNumericIdGenerator::get()->next(); > ``` > > =E2=80=94Claude > > You've overcomplicated your example, but the truth is that there's sometimes a reason behind the complexity you might see. In this case your functions will work right until you need two independent sequences, or you need a backwards direction, or you need a sequence that can be seeded with a "starting point". The point is not to have the "OO way" because someone likes it, the point is to provide more flexibility, one that is likely to be needed down the road. In this case, a class like Number that accepts a specific $base in the constructor will not hurt. There are definitely use cases for custom base numbers as mentioned above and a class is just a better way of doing it than trying to combine multiple concepts (base-10 ints, text strings and custom base numbers) under one or two types and a bunch of functions on top. --000000000000e0050305f28fa68c--