Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125022 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id C43C71A00BD for ; Sat, 17 Aug 2024 04:54:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1723870557; bh=e/o9zA4bW6YBKrpbxqyV4CM7jbov4UTjplzs8Y6esX8=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=WM4PD7WInFsBvl8kMajSjaXYkG8i0dZV3v1Zc+i4ddgwJ8HQoFfK5qCVw5vHfDiIs KvqK2SrjBjd1Br0J//LrKei2Tz09wGLa7OOXKd89RFgmDD94/yehuBi2TNkQLEHuM/ NVd5gDUnIhdpRlS8McRA2+UbBRKLEsg6CLK559XLxICDYUiOXPzaZa2KumhWXqid/p 6P+0DTSrVVCcM1SPQ0QwQiAwzXWMoPMq8fnaKGfzZ3zv61kfp46yWqLtZIOcD0kWY7 gFl2jU+ytJqH9EDbgK9ECIfU6DgwxMOkUzlDbY8T2ZUeNrzBtgE3XZtwKBEuiJq3Wg eJ+4fEkB0cBiw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 02C27180077 for ; Sat, 17 Aug 2024 04:55:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 17 Aug 2024 04:55:56 +0000 (UTC) Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-e0875f1e9edso2627861276.1 for ; Fri, 16 Aug 2024 21:54:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20230601.gappssmtp.com; s=20230601; t=1723870448; x=1724475248; darn=lists.php.net; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DwK7z4EjCGlwJFUtgNdBTgzZQ8sLaPwz0eCIc2m4tds=; b=aKdoBJ7f5TNu8EonudrfuE8PiqhvkSn155t/xIv5em5ASdBqUHFppXUQHvS+P4dqXn Ghtg5xeEwM8hqrSmKUbobMgicapnmzJaswMwD8aaiMPudxLV6EeAFvuBRM4a4aek8Nvn xouCxdYJwCjkwfA4Pp1AKT2j/ebIne+y38T28k9DXln4OkJglzhbxfrrRQ/RySbUvJ+q 9ftdnqqOvyn8VkpKZUaNDOPUzl3LtLD1r+0H7XFz6scDGPfjVeYalOA/cob14XzI9Hvq yS6VMVWF7Pjou3f5/X2VyI5rrmUReCRrFDs5Vb73euHzxI+WPEM3bUl+AQOFb2fGrmnz FfMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723870448; x=1724475248; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DwK7z4EjCGlwJFUtgNdBTgzZQ8sLaPwz0eCIc2m4tds=; b=r2rOUhYx7cpPmOVYAFM6MbTLfmknXXJI/iwvK0Sm5QBFX3crRgTJwWBF8YJtrqvWkU c93JnRejFiWsKwtKVOp71HZonQsd/kQmSfuDUvInAyW0Obrx2TVaaJRn767zneU9XzlH +LtjySsSDLkgEDcoB0fwwllTCXaYgsdy/bIbrus0XpxLjw/zmtxengbAlZWfRxoJ5sNP 30jyR58e9AKCZikimLUZyyb9LZF1AllhoZ1pOBP9fXT/ewQYa3nC5dTapkkIald+Es0A 3gKcGW242qDlj6/UDXGEyeIlCeVhaxmT3AUom79LC6PH8xua9PU5fHGxilAz8By9SNxg j/Qw== X-Gm-Message-State: AOJu0YzhZxr2Uxev1AK4tRgZClHu8YWF7b6mVKgwlOZ3JW3vWOA1mKB0 Pbb0AIQajC5RaG/YSlVngYfJblxA6Iwdtc80md2OtAKJ8qIKl7PgWQK3SKYCTHY3lFhj4Ge5bQh FCAI= X-Google-Smtp-Source: AGHT+IFgsDzUjf66l6yDruB64NO6OKGiIiSqiChKWZMM3US8ktRr8lfJUAaKkIcci9wZL2j4+cqeYQ== X-Received: by 2002:a05:6902:2011:b0:e08:664c:dd23 with SMTP id 3f1490d57ef6-e1180e6c645mr5985802276.11.1723870447928; Fri, 16 Aug 2024 21:54:07 -0700 (PDT) Received: from smtpclient.apple (c-98-252-216-111.hsd1.ga.comcast.net. [98.252.216.111]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e11720092ddsm1123807276.42.2024.08.16.21.54.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 21:54:07 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.8\)) Subject: Re: [PHP-DEV] Should there be a `get_declared_enums()` function ? In-Reply-To: <66BFFC0C.1090900@adviesenzo.nl> Date: Sat, 17 Aug 2024 00:54:07 -0400 Cc: PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <66BD5F05.8010409@adviesenzo.nl> <8dc51539-20bb-4017-b859-76066e2287e5@gmx.de> <4AA66A3E-1ACF-48F5-81A2-C6AAAE8B3FD0@newclarity.net> <66BFFC0C.1090900@adviesenzo.nl> To: Juliette Reinders Folmer X-Mailer: Apple Mail (2.3696.120.41.1.8) From: mike@newclarity.net (Mike Schinkel) Hi Juliette, > On Aug 16, 2024, at 9:25 PM, Juliette Reinders Folmer = wrote: >=20 > On 17-8-2024 2:00, Mike Schinkel wrote: >> To everyone on this thread and thus nobody in specific (hence my top = post): >>=20 >> Given that get_declared_*() populates a new ZEND_HASH each time the = function is called[1] =E2=80=94 which for classes can easily be hundreds = of strings =E2=80=94 would there be any appetite to:=20 >>=20 >> 1. Add an optional parameter =E2=80=94 e.g. = `get_declared_classes($startingFrom)` =E2=80=94 that can limit the = number of symbols starting with "n" where if n=3D=3D100 then it would = return array_slice($symbols,100), and=20 >>=20 >> 2. Add a function =E2=80=94 `get_declared_class_count()` =E2=80=94 = that returns the current number of declared symbols of whichever kind of = symbol, so we can get the "n" before calling `include`/`require` to use = with get_declared_*() after calling `include`/`require`? >>=20 >>=20 >> This would cut down on creating hashes with 100+ strings that get = immediately thrown away, and also cut down on memory = fragmentation/garbage collector churn.=20 >>=20 >> -Mike >>=20 >> [1]=20 >> = https://github.com/php/php-src/blob/18d41502da0da1bb3928e60c41f1b821974c2c= 01/Zend/zend_builtin_functions.c#L1371 >=20 > Mike, >=20 > While your suggestion does not directly relate to what triggered my = mail to the list, I very very much like the idea. Excellent! > It's as if you are reading my mind or rather, reading one of the = autoloaders [1] I need to maintain, in which your above outlined = proposal should definitely make for more efficiency. Thank you for sharing that. Yes, your use-case is very similar to mine.=20= > Having said that, the same parameter + extra function would then also = be needed for `get_declared_interfaces()`, `get_declared_traits()` - = and, if it would be added, `get_declared_enums()`. That was my assumption and intention, but I did not say so explicitly. I = should have. Thanks to Ayesh's excellent PR, I was able to recognize that all of = those functions in-fact are implemented via one internal function = `get_declared_class_impl()` so it only made sense =E2=80=94 to me, at = least =E2=80=94 that modifying it would enable the same functionality = for all four (4) userland functions. > There is one problem I can see with this approach though: since PHP = 7.4, the return value of `get_declared_classes()` (and friends) does not = guarantee any particular order anymore [2]. > So, an `array_slice($symbols, $n)` may not get you the _latest_ = classes loaded, so I think this would only work if the order of classes = is guaranteed in some way. That is a super relevant insight and one I was actually not aware of. Do you happen to know if it was triggered by an explicit RFC to that = effect, or if it resulted because of a side-effect of some other = implementation change? =46rom my perspective, that is definitely a BC = break and one that I am surprised I never noticed before? Do you also = know if it is a case of "not guaranteed, but in-fact actual fact it = always works that way, or not?" =20 I have not used my lib that needs that functionality to work in a few = years, so maybe it is broken now and those who are now maintaining those = apps have had to deal with it? Given that you see value in the same use-case I had which was an = autoloader, maybe it would be better to achieve the functionality we = need in a different, more efficient way? =20 What if instead PHP were to implement an optional 2nd callback parameter = to `include()` / `require()` / `include_once()` / `require_once()` to = allow us to capture the symbols loaded and their paths? The callback = function could return `void` and accept an array of `$symbols` with the = following guaranteed minimum structure? $symbols =3D array( 'classes' =3D> [], 'interfaces' =3D> [], 'traits' =3D> [], 'enums' =3D> [], ); With that you could reimplement all that PHP code you linked =E2=80=94 = at least for versions of PHP supporting the callback =E2=80=94 to look = like this: function loadFile($path) { if (strpos(__DIR__, 'phar://') !=3D=3D 0) { $path =3D realpath($path); if ($path =3D=3D=3D false) { return false; } } if (isset(self::$loadedClasses[$path]) =3D=3D=3D true) { return self::$loadedClasses[$path]; } include($path, function($symbols) use ($path) { foreach ($symbols['classes'] as $className) { self::$loadedClasses[$path] =3D $className; self::$loadedFiles[$className] =3D $path; } }); return self::$loadedClasses[$path]; }//end loadFile() This approach would be a less disruptive than my prior suggestion which = would have added four (4) new `*_count()` functions and new optional = parameters for four (4) existing function vs. this proposal which just = adds new optional parameters for four (4) other existing functions. = Further, and the signature(s) required for parameters would be much = easier to settle on. =20 I checked the code[2] and if I understand it correctly would (only?) = require adding a Zend Observer[3] callback to be notified in = `zend_bind_class_in_slot()` although I still cannot figure out where = that observer would need to be added. Going this approach would also make the userland code in PHPCodeSniffer = a lot more efficient than the code you currently must run for every = included file simply to determine what symbols were loaded. What do you think if this alternate approach? -Mike=20 [1] = https://github.com/php/php-src/blob/18d41502da0da1bb3928e60c41f1b821974c2c= 01/Zend/zend_builtin_functions.c#L1371 [2] = https://github.com/php/php-src/blob/php-8.3.10/Zend/zend_compile.c#L1231-L= 1269 [3] = https://www.linkedin.com/advice/3/what-some-best-practices-common-pitfalls= -using-zend