Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113284 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 19478 invoked from network); 26 Feb 2021 11:35:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2021 11:35:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7B3E218053F for ; Fri, 26 Feb 2021 03:24:35 -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, 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-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 03:24:35 -0800 (PST) Received: by mail-wr1-f49.google.com with SMTP id r3so8212918wro.9 for ; Fri, 26 Feb 2021 03:24:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=5sDFzvazRCrrfLYIsunhbBhmQW80IXS8GctDgipg5rg=; b=s+ZL6Hm+nAUgQCh2KpYgl1GwY4qrjvLlWXsBZY4zoZkzfGStGlXqKkfldSfniTmdvW 1U+dtcSZLX1PJKtP9BJaJnucIrPnXocyFKu22LVNOpFnC/Aft52lp7IDJ1S6zIU+0QBV Fm/6ul/ws5I4pFJ10YC8Rf2NNNz8aamwtbwNcigAB1WoSPfhkxb4NSO/OlTLfFhCphd/ K0+fv9fjHUDDE+ESikAuZvhUN5PjjH/Q7W80erRziHj5qxB2FlnMDxw2sNdHvNXnZQbn IWUTF9FiriA5sjYKS+639q5ByTEQJ2kG1Q995xk6S/uoQrxNmXSRzSSZLXZdrBCh0D5J 7yCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=5sDFzvazRCrrfLYIsunhbBhmQW80IXS8GctDgipg5rg=; b=N7HQPs0hD/xJ/SdRxer5U7yy9SJaBdEjwfTrRJ9FJUxbXjYwu2tCM9CA07fYIRIaYu EErGGgwRIcu1YYYxodZSVxFiuSQeduXMgeB9QCkyQJhuRGYH1JMRd0Ytsc8tgfGSgmrZ rGSjV6oQyZYODIcH0nmLJ7h0hesuu0429joNy26ElRcHlZ/OqKgfCABA37SzPPWWLTBC xEYtiA7t6DuyZkdi0wZHFZA2J6rtgxx6llL4PhKZzH4r0CbeP/70KTcercj5ZemsbnE5 5GAfixjfn/IQnTwZacFL6NdBgdQ2VsrVC3/rNrQDgAzhVTWgFqos0qMUmb5hTcm+ogSN JvCQ== X-Gm-Message-State: AOAM530FKT0de7SUY95r48JX2+ohAT5x76MM/4kozW5CCWXafSTrm6dk iRwvk+ariSH591fjiXqxIfk= X-Google-Smtp-Source: ABdhPJyFCnoTwZdkENrLGZkGvToM4nTC2eFbRGZz9vlZ0SGhmN9D15FV5cfpzDBFcUWLInIvv9UUCw== X-Received: by 2002:adf:e791:: with SMTP id n17mr2693183wrm.322.1614338672402; Fri, 26 Feb 2021 03:24:32 -0800 (PST) Received: from claude.fritz.box ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id c3sm13527161wrw.80.2021.02.26.03.24.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Feb 2021 03:24:31 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: <177dd68f109.c748581d17565.7200783349436492456@manuelcanga.dev> Date: Fri, 26 Feb 2021 12:23:53 +0100 Cc: David Gebler , Internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <177db7bbf72.ecf4615f732310.8822018854991483226@manuelcanga.dev> <177dd68f109.c748581d17565.7200783349436492456@manuelcanga.dev> To: Manuel Canga X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] [RFC] class_name:namespace From: claude.pache@gmail.com (Claude Pache) > Le 26 f=C3=A9vr. 2021 =C3=A0 09:16, Manuel Canga = a =C3=A9crit : >=20 > Hello, another example with "factories method"[1]: >=20 > ```php > use MyProject\Framework; >=20 > abstract class AbstractController { > private const HELPER_PATH =3D static::namespace.'/Helpers'; > private const SERVICE_PATH =3D static::namespace.'/Services'; >=20 > public function instanceHelper( string $helperClass ) { > $helperClassName =3D self::HELPER_PATH."/{$helperClass}"; >=20 > return new $helperClassName(); > } >=20 > public function instanceService( string $serviceClass ) { > $serviceClassName =3D self::SERVICE_PATH."/{$serviceClass}"; >=20 > return new $serviceClassName(); > } > } >=20 > use MyProject\MyModule; >=20 > class Controller { > public function __invoke() { > //...... > $date =3D $this->instanceHelper('Date'); > //... > } >=20 > } > ``` >=20 > [1]: https://en.wikipedia.org/wiki/Factory_method_pattern >=20 > Regards > -- > Manuel Canga >=20 > Zend Certified PHP Engineer=20 > Websites: https://manuelcanga.dev | https://trasweb.net > Linkedin: https://es.linkedin.com/in/manuelcanga >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php 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