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 = "My\\Namespace\\Name\\Class";
var_dump(preg_match('/^(.*)\\\([^\\\]*)$/m',$foo,$matches),$matches);

array(3) {
  [0] =>
  string(23) "My\Namespace\Name\Class"
  [1] =>
  string(17) "My\Namespace\Name"
  [2] =>
  string(5) "Class"
}

Regards
David Gebler

On Thu, Feb 25, 2021 at 9:40 PM Manuel Canga <p...@manuelcanga.dev> wrote:

>  ---- En jue, 25 feb 2021 21:41:40 +0100 Nikita Popov <
> nikita....@gmail.com> escribió ----
>  > On Thu, Feb 25, 2021 at 8:11 PM Manuel Canga <p...@manuelcanga.dev>
> 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 = explode( '\\', MyClass::class );
>  > > array_pop($splited_class_name);
>  > > echo $namespace = implode('\\', $splited_class_name );
>  > > ```
>  > >
>  > > Other option is:
>  > >
>  > > ```
>  > > namespace MyProject\MyHelpers;
>  > >
>  > > class MyClass {
>  > >       public const NAMESPACE = __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 compiler
>  > > 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 = '/views/';
>
>    protected function includeView( string $viewName ) {
>         $filePath = str_replace('\\', '/', static::namespace ).
> self::VIEW_SUBPATH;
>         $fileName =$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
>
>

Reply via email to