Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113290 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 38178 invoked from network); 26 Feb 2021 13:51:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2021 13:51:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5D6E71804E2 for ; Fri, 26 Feb 2021 05:40:17 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 ; Fri, 26 Feb 2021 05:40:16 -0800 (PST) Received: by mail-lf1-f48.google.com with SMTP id z11so13825320lfb.9 for ; Fri, 26 Feb 2021 05:40:16 -0800 (PST) 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; bh=2J2HWoYR+kVDh1Xgm0aPct7UAfhoZcd/pTKkLDPatag=; b=Hj7pxBljPwfcPz7001l5MwcIyMOkEd+aQTfViHo7hdKTqWAWMsppnNremRO2aNPZzi +oLQJGH/WrXL0e6ev8HVF2iwsJ08nj4yNFcGc3NoOhZv3o9ZUxrKqe43AhzvoSFTpszb g6285QvHPBbHqS1aaxlJwcfpvqZ3oWNwfEt9qrhfz/9LtlWOlG58tueSOSouXIaW9Szk 7udpAIHygQcaP7ySErfIDcpYtsXZ+RpoDdJWPf8scUJPrs4mSpDqm8hnz1nqjsYGTUOp mIdWp4nSGYxSdaz1gnFPe5LVv0j+FtCp+MxUm3HYG8YAObOw/JGhU4LnzGgOzrCiuaRT Meyw== 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; bh=2J2HWoYR+kVDh1Xgm0aPct7UAfhoZcd/pTKkLDPatag=; b=Igddd+fN4ghLbyFG1EOCcJMtZyZmtMwEzwyjLtQvYMB7tdwoXxJthzA9/8yOXmY0Fg QjdO8G6VtBqc7oJrRikHrTk1K1DTWjosFeDg8F1QQzp7cVlSMHZiM49zLFLBxcmpS5r+ 6Cc4y3BxzYDwm8/5tIIDcFq9mDezX2fAdw0913XgVk3pHt+EkU4COgyNC5uxxq+2Chd+ rmShvl3ZU9r+6XP0/JD4MiMm03D8DMximnRd6EGASAn4bhK0G35CmyZJBI6OX49CJ8ZM 7zinBQEQfiIN2iJCQgotv8s/1bJmSdUeKcEKk3qjDE1JLBT8n5coKc1NlOKyWVIiUUqo Wxuw== X-Gm-Message-State: AOAM530mQFv8mfuyIDOTzWNVL5vISjZdz87FBZENOJ3MZ2i/x7+1b6vF HB8mp6umU2I7+WomBxUsyhIla9vildrahkH71Qlt3cZJlxXG X-Google-Smtp-Source: ABdhPJzSlGwtcnEWuOhh/sQAxjTv+1PVRQO0dOa1TeFhV5kdpwkZvh1As8FO5igkXnqUQm7mPpuiOMoiRulLbQTesag= X-Received: by 2002:a05:6512:4c9:: with SMTP id w9mr481904lfq.335.1614346815226; Fri, 26 Feb 2021 05:40:15 -0800 (PST) MIME-Version: 1.0 References: <177db7bbf72.ecf4615f732310.8822018854991483226@manuelcanga.dev> <177dd68f109.c748581d17565.7200783349436492456@manuelcanga.dev> <177de1ff1a0.feee271246706.3311842089729077831@manuelcanga.dev> In-Reply-To: <177de1ff1a0.feee271246706.3311842089729077831@manuelcanga.dev> Date: Fri, 26 Feb 2021 14:40:03 +0100 Message-ID: To: Manuel Canga Cc: Internals Content-Type: multipart/alternative; boundary="00000000000077446d05bc3d688b" Subject: Re: [PHP-DEV] [RFC] class_name:namespace From: guilliam.xavier@gmail.com (Guilliam Xavier) --00000000000077446d05bc3d688b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 26, 2021 at 12:36 PM Manuel Canga wrote: > > > > Hi, > > > > Note that (typos aside) this example cannot work as-is, because > `static::namespace` (like `static::class`) cannot be resolved at > compile-time, and therefore cannot be assigned to a constant. > > > > More generally, in the various examples you provided, the namespace is > not resolved at compile-time: it is either static::namespace or > $fullClassName::namespace where $fullClassName is provided by the > autoloader. As such, instead of a ::namespace magic class constant, it > might be more appropriate to have a helper function, say `get_namespace()= `, > which has the additional benefit to work not only on (fully qualified) > class names, but also on function, constant and namespace names (in the > case of namespace names, it would return the name of the parent namespace= ). > > > > =E2=80=94Claude > > > > > > Hi, Claude, > > That's sound great. Something like dirname[1] but with namespaces. > > But...in that case, I also add a new function like basename but with > namespaces. Example autoload for WordPress : > > ``` > function autoload( $ful_class_name ) { > > $class_ path =3D str_replace('\\', '/', get_namespace( $ful_class_name ) = ); > $class_name =3D strtolower( get_base_namespace( $ful_class_name ) ); > > require "{$class_path}/class-{$class_name}.php"; > > }``` > > [1]: https://www.php.net/manual/es/function.dirname.php > [2]: https://www.php.net/manual/es/function.basename.php > Hi, For that example (and most others), you're manipulating file paths anyway, and assume that the FQCN is at least one level deep (i.e. not top-level), so you may as well start with `$p =3D str_replace('\\', \DIRECTORY_SEPARATO= R, $fqcn);` then use `dirname($p)` and/or `basename($p)`. Now for the (rarer) cases when you actually want the namespace name and/or the unqualified class name, I agree that the current state is not ideal. To my knowledge, you have the choice between "magic" string manipulation (various ways) and "semantic" reflection (e.g. `$r =3D new \ReflectionClass($fqcn);` -- which may try to autoload the class and throws if it doesn't actually exist -- then use `$r->getNamespaceName()` and/or `$r->getShortName()`). Like `str_contains()` was introduced as a "shorthand" for `false !=3D=3D strpos()`, something like `get_namespace_name()` and/or `get_short_name()` (or other namings) might be worth considering. Regards, --=20 Guilliam Xavier --00000000000077446d05bc3d688b--