Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113274 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59663 invoked from network); 25 Feb 2021 22:23:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Feb 2021 22:23:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 736591804DD for ; Thu, 25 Feb 2021 14:12:39 -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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (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, 25 Feb 2021 14:12:38 -0800 (PST) Received: by mail-ot1-f43.google.com with SMTP id e45so7242695ote.9 for ; Thu, 25 Feb 2021 14:12:38 -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; bh=URWo+zRoitfOrb/xw9SQnvUq2HQw2Nwqtr74sAjYSI8=; b=SfeOu8XGQaYf9QXK14w3Qbo7SSQx3HME2MSZg4/Es4Kr5wu/s7Xk1hBWLDh4CjFqX7 LXuOoyB+hqJtPzwahYAHej9ORjOV1AdUyXIzsPrS3qAO5XNX7z6xW1wxQgDr1a27CKIA zN0vY3CbFCEO8w/U78JQ/6xHRZOA5rySOBXdjRbkvT6i6p9IaAHLyN56BIZ0pVNk612m rSfhtB689RLUfIWc73CthXTUYEeR/9X29UG3N+c5CdagxNhXCHPMcaHWH1dPhaCMc7PS Qt0UjVDSf6PG4weNgpI6DR2S730vP0O5/WDWOFHFkzPdM9zemeEXsoNkXR1HS7bKCy7V uSLg== 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; bh=URWo+zRoitfOrb/xw9SQnvUq2HQw2Nwqtr74sAjYSI8=; b=gJy41jKRVqM7SOwlJnqSS2B0UVVpMfQ0/0iTjacJ2a1kYhHYHraOFb0xUCVXokJRSC OkWyJB4MCF67GUb+fhQ8baEseMHwJDhQLA7ZJ+OEFVUf/QeKcVRV+qv6vUy7FLZaCADA KNgRyRvJYgLx0RyZ0WZY4PVkzeKXzE/alGsTzhVQaSwG9rW9HmJkmDy+jzW/n2a/04mq uX/LITjW3jUpIKUvMRc5lX7KA2i+sU+2nc0R51v+OmRZ+eAQGNMFITSvaDxm02VFE8gH EMpheTM9OvlM7iMV8lLX1IISzFNNK9rBMMf1Qe/Fn/ks4apobr63vOlb5o/Mx73y1Pnn 532A== X-Gm-Message-State: AOAM532mfWHTRx76ti6cifsr+0GlRu3xplPcCO6PzCVwvesRLUT82+Zs vVFYNfKwLVybx8lkDrN+wMhVcyK1CQZ05YsKcdYqDxCmD7DPJQ== X-Google-Smtp-Source: ABdhPJxHtv5LAOErI1cWaxFf3g5s+q9+fP+MnsSFveLIpnMJEUKsTJm+LaU9OmhHCSYzm1NVmT1N7OrEUlAuCYirzGU= X-Received: by 2002:a9d:6381:: with SMTP id w1mr4005040otk.236.1614291153993; Thu, 25 Feb 2021 14:12:33 -0800 (PST) MIME-Version: 1.0 References: <177da98a052.c500761c696204.3345368100437568281@manuelcanga.dev> <177db219176.d0703af5721028.3912178391371376880@manuelcanga.dev> In-Reply-To: <177db219176.d0703af5721028.3912178391371376880@manuelcanga.dev> Date: Thu, 25 Feb 2021 22:12:23 +0000 Message-ID: To: Internals Content-Type: multipart/alternative; boundary="000000000000cc440605bc307246" Subject: Re: [PHP-DEV] [RFC] class_name:namespace From: davidgebler@gmail.com (David Gebler) --000000000000cc440605bc307246 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You can achieve what you're trying to do already with a combination of get_class() on a namespaced class and a simple regex (or other method of processing a string to your liking): $foo =3D "My\\Namespace\\Name\\Class"; var_dump(preg_match('/^(.*)\\\([^\\\]*)$/m',$foo,$matches),$matches); array(3) { [0] =3D> string(23) "My\Namespace\Name\Class" [1] =3D> string(17) "My\Namespace\Name" [2] =3D> string(5) "Class" } Regards David Gebler On Thu, Feb 25, 2021 at 9:40 PM Manuel Canga wrote: > ---- En jue, 25 feb 2021 21:41:40 +0100 Nikita Popov < > nikita.ppv@gmail.com> escribi=C3=B3 ---- > > On Thu, Feb 25, 2021 at 8:11 PM Manuel Canga > wrote: > > > > > Hi internals, > > > > > > I would like to present a possible new RFC( "class_name:namespace" ) > for > > > your consideration. > > > > > > As you know, namespaces are very important nowdays. They are used in > > > autoloaders, Frameworks, CMS, ... > > > > > > Maybe, you are used to code something similar to this: > > > > > > ``` > > > use MyProject\MyHelpers\MyClass; > > > > > > echo substr( MyClass::class, 0, strrpos( MyClass::class, '\\')); > > > ``` > > > > > > or perhaps: > > > > > > ``` > > > use MyProject\MyHelpers\MyClass; > > > > > > $splited_class_name =3D explode( '\\', MyClass::class ); > > > array_pop($splited_class_name); > > > echo $namespace =3D implode('\\', $splited_class_name ); > > > ``` > > > > > > Other option is: > > > > > > ``` > > > namespace MyProject\MyHelpers; > > > > > > class MyClass { > > > public const NAMESPACE =3D __NAMESPACE__; > > > } > > > ``` > > > > > > However... :( > > > > > > ``` > > > namespace MyProject\MyServices; > > > > > > class MyNewClass extends MyClass{ > > > } > > > > > > echo MyNewClass::NAMESPACE; //MyProject\MyHelpers > > > ``` > > > > > > All of these examples are ways for getting a thing which PHP compile= r > > > would resolver fast. > > > > > > It would be fantastic can code: > > > > > > MyClass::namespace or static::namespace( for example, in abstract > classes ) > > > > > > Don't you think the same ? > > > > > > Could you please share the use case(s) you have in mind for this? > > > > Regards, > > Nikita > > Hi, Nikita, > > Yes, of course. For example, loading views using TemplateViews pattern[1]= : > > ``` > namespace MyProjects\Framework; > > abstract class TemplateView { > private const VIEW_SUBPATH =3D '/views/'; > > protected function includeView( string $viewName ) { > $filePath =3D str_replace('\\', '/', static::namespace ). > self::VIEW_SUBPATH; > $fileName =3D$filePath.$viewName.'.tpl'; > > if( file_exists($fileName) ) { > return include $fileName; > } > > error_log("Not found view[$viewName] in path $filePath" }; > } > } > ``` > > ``` > namespace MyProject\CMS\Freelancer\Attachments; > > class Budget extends TemplateView { > > public function __toString() { > $this->includeView('full_budget'); > } > > }``` > > > > Regards > - P.S: Sorry, my mistake with subject. > > [1]: > https://dzone.com/articles/practical-php-patterns/practical-php-patterns-= 9 > -- > Manuel Canga > > Zend Certified PHP Engineer > Websites: https://manuelcanga.dev | https://trasweb.net > Linkedin: https://es.linkedin.com/in/manuelcanga > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000cc440605bc307246--