Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116614 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46245 invoked from network); 9 Dec 2021 20:25:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Dec 2021 20:25:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9AD8518054A for ; Thu, 9 Dec 2021 13:26:02 -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=0.0 required=5.0 tests=BAYES_20,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.17.13]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 9 Dec 2021 13:26:01 -0800 (PST) Received: from oxbsltgw65.schlund.de ([172.19.249.155]) by mrelayeu.kundenserver.de (mreue108 [213.165.67.113]) with ESMTPSA (Nemesis) id 1MBUyX-1mpdDz13k7-00CyL1 for ; Thu, 09 Dec 2021 22:26:00 +0100 Date: Thu, 9 Dec 2021 22:25:59 +0100 (CET) To: "internals@lists.php.net" Message-ID: <1313421219.904906.1639085159906@email.ionos.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer v7.10.5-Rev25 X-Originating-Client: open-xchange-appsuite X-Provags-ID: V03:K1:9+xagsVTHEmhGAeymif2TUEKHD3fMbjuQkpxt7lpnlYx/EVVeoa AXPXIbTKF6qBs4Rc3hy4bDPWmqw+ctS3lXWuFii31HYryFtl8WkgmcvFYXDeMGA511h+Pcu yIbCGd24YMY1JfkX9PPRiRq3vHzoGPS8CAloIoWX+gZFFMnvVbAacPaICMj15XlTU1htw8Z 8azK6vzFJDTkB3kg94Iqg== X-UI-Out-Filterresults: notjunk:1;V03:K0:FpwRd4zmQqw=:6tOB6k03bj+0JO5M9sRbcv qG2t/IBpM6/4/ZnicI5Pk3jar7FT6ty3eEzigcNJ6Apkl2x7LnEIzTbG/eY3yBZl2hLeyMyKM xLjHltWsNk2o6GTHxm7z8sCX3L3p8qqxPuRtPJ9uJIlrNniiXN2VJaEWG688FKU+NBuTsz8zs lIoYwcqI2OwoZC3hj8LFPFkvuZSNiL1zzWkvMsIq7XNzS32Iadrv6DtwjRmo0zfOcDEFiJbo8 T2JZoY9kpr6d6J2dv59A+psX9T3CFqC72btUruGElyDC6vRAdpC24sU8Dxz9Bo1jcWr86Wg3e 5QXIv9KYwMlGepkGm68lXHZjHRG62WdqAUqJiKsEwE84Cj8PPOOf1W04yfypMjbcRE4TsHVaS WBKBlLr9VQbzqnamvh/aaVfXSZUXPsswutMfHtx1GeBdrgOktjml/vLWf9AKj3bXdTg0naTe4 yZEJwocthYZy8+19NatgAdpPjln3dTFN8CiHO5n2+jVyKqi0uBNmIWLMr/v4gsl0q/rziV4u4 zmFrjzTCHdcdVI31cqEOOoZgXYECqXsQlF/svCzqKKdL+TvXmQ3PaAyQK7wHswJiLqy9APwbP DvdwRMPWV/vcBpqwcyiW+eVxU+15VH+a2mFQa9/ZHXUIU7VKme2Zum/EGuMN8UqeSqjIXD6/5 wT7yeMCqo09cAhIlK5/RRa+40nNJuiqK4hYfk3p+3w/2Z+5+M6eAPS3IBbdTOVr4WEdk= Subject: Discussion array_get_type() From: mails@thomasbley.de (Thomas Bley) Hello, since there are currently no type declarations for specific arrays (int[], string[], MyClass[], etc.), I'd like to ask if it would make sense to have a function array_get_type() in php core? As I'm not a C developer, I can only ask for your help for the implementation. I think having it in the core will provide much better performance, esp. when the result could get cached on first evaluation (and the cache is invalidated when the array is modified). function array_get_type(array $array): string { $result = null; foreach ($array as $elem) { $type = get_debug_type($elem); $result ??= $type; if ($type != $result) { return 'mixed'; } } return $result ?? ''; } echo array_get_type([1,2,3]), PHP_EOL; // int echo array_get_type([0.1, 0.2]), PHP_EOL; // float echo array_get_type(['a','b']), PHP_EOL; // string echo array_get_type([true,false]), PHP_EOL; // bool echo array_get_type([null,null]), PHP_EOL; // null echo array_get_type([]), PHP_EOL; // '' echo array_get_type([new stdClass, new stdClass]), PHP_EOL; // 'stdClass' echo array_get_type(['a',null]), PHP_EOL; // mixed echo array_get_type([1,0.2]), PHP_EOL; // mixed echo array_get_type([new class {},new class {}]), PHP_EOL; // class@anonymous echo array_get_type([function() {},function() {}]), PHP_EOL; // Closure echo array_get_type([new DateTime(), new DateTime()]), PHP_EOL; // DateTime echo array_get_type([new DateTime(), new DateTimeImmutable()]), PHP_EOL; // mixed Having this function would make it easier to check types of arrays and avoid things like array_sum([1, 'a', 'b', 2]); Best Regards Thomas