Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110493 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 3578 invoked from network); 12 Jun 2020 05:47:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jun 2020 05:47:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 47D111804C7 for ; Thu, 11 Jun 2020 21:32:14 -0700 (PDT) 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.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) (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, 11 Jun 2020 21:32:13 -0700 (PDT) Received: by mail-io1-f52.google.com with SMTP id s18so8932041ioe.2 for ; Thu, 11 Jun 2020 21:32:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=xvTlKr2rU7IEvGdRobkEgWo6yiP0O/2XkST+UlhMjJs=; b=H6zRtTW4DuasvmUPQzw9RlPXNBpAZwReVahP620eEITkG1pT6VJd9bI5me2G1WCMoI sRF8OrMcVO3Ea/0253FdXIBNvoY8afJRJ9jfDHGsbWeV+BkR1gbMfNuS89ApxbddxR+2 adKzNGniChoopn39GCfxciynBXBT2pTxpTm23NdN+65qzW1SGNjgM8O0lcSINgSGspi/ rwxKzR6YQHQomDXDVelS72Zc7EhR3tplcU20qHarOleTHOjx2Xa4OKZ+y9uAEn5wCohV 6KZ7CaItBzo7xgd47MWV8PvcAXbn97U0VynhP8eIfiZEElnDW2kj0B+7AAyhYBA673I6 26Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=xvTlKr2rU7IEvGdRobkEgWo6yiP0O/2XkST+UlhMjJs=; b=ZBIpzvb4X50qfF/IxeRWXAJYQJp80T5YONBFFD0Zl3nQRk3ebAypOsf3bx48GewWEC yXiR6qS/yFuxtLeCLXxpJtpblUi7QUIQYPA3WrHYzEgoxKir3ABM035+bVGnC/5p5YmX MGVN84kHb3exHroMjhxKwaFStJ1j5zKVbabru9SxUSSvPZGTSJeUS6DvOR0hr3DO+Dkh vQd3xe0YHrCMqEZBP7s+0GRl1VL+ZenA4PTj7DunmF+KV4cJP2pJu5Gt1vxUtsUBoTgA FSSH5W0TTx4g9hmn28Z6wmuJFR74OrlL60Z1AMQ0nkNjAfL0oNPZWdjH5RJztSDaEBjs Q8EA== X-Gm-Message-State: AOAM533ElM8gV0taLmR2W5go59UuZoSh3/Qg8F0uOLpNFOivbaKdxg9E elehI8Tj9ofov4BPolNnW2X55FsOPXlSUK/P2ZI= X-Google-Smtp-Source: ABdhPJyDMDEUGpc8WLGp1lo6erqK8EIQz4QjXBByyoiId2jTC25GSUf39B7olOwivGLLFNHMxMt1oUT5UCkmiZeL5Y4= X-Received: by 2002:a6b:b503:: with SMTP id e3mr5276532iof.175.1591936330134; Thu, 11 Jun 2020 21:32:10 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 11 Jun 2020 23:31:58 -0500 Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Revisiting proposal for addition of `get_class_constants()` From: troy.mccabe@gmail.com (Troy McCabe) Hey Nikita, Thanks for the thoughts. > Could you please explain in more detail *why* we should duplicate existin= g reflection functionality into free-standing functions? In terms of the *why*, there were three main reasons: 1. It aligns with the addition of the functions referenced in the original post (`str_[contains|starts_with|ends_with]()`), and one their stated reasons of simplifying the API for userland developers. While `(new \ReflectionClass(MyClass::class))->getConstants()` isn't the most difficult thing to grasp, it's not immediately clear to new developers, and is more verbose than `get_class_constants(MyClass::class)` 2. `get_class_[methods|vars]()` existing as built-in functions, creates a gap to retrieving class constants in the same way. If I start down the path of class inspection using `get_class_*()`, but find I can't retrieve constants in the same way, this is an inconsistency. 3. When using Reflection, accessibility is not respected as it is with the `get_class` family of functions. In the event that a developer is looking for constants which are accessible to the current context, there's no way (that I'm seeing, anyway) to retrieve _only_ constants accessible in the current context. > I believe the existence of functions like get_class_methods() is a histor= ical artifact, because they were introduced before Reflection was a thing. = Unless there is a strong reason to the contrary, I would prefer reflection = functionality to stay inside Reflection... This is good background that I wasn't aware of (I knew the Reflection API was newer than the built-in functions, but not that the `get_class_*` functions were generally frowned upon). It does bring up 2 questions: 1. Obviously this is a much larger discussion, but is there any appetite to deprecate & remove the existing functions in favor of the Reflection API? 2. An alternative to adding `get_class_constants()` would be to introduce `ReflectionConstant` as a return type from `ReflectionClass::getConstants` to match `ReflectionMethod` & `ReflectionProperty`, which would solve point 3 above. Would this be a preferable approach? > You do mention performance as a benefit, but it's not immediately obvious= to me which use-cases are bottlenecked by class constant reflection. Enum implementations are the big case for this. While the libs I've looked at use an internal cache, these caches are per-request, so reflection will need to be used as many times as there are enums in a given system. Depending on the scale, this could be an appreciable amount. Obviously external caches could be leveraged, but that then requires additional development lift, instead of using battle-tested enum libs. Thanks for the thoughts, and thank you for all your work on internals! Thanks! Troy McCabe