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 ?
Regards
Manuel Canga
Zend Certified PHP Engineer
Websites: https://manuelcanga.dev | https://trasweb.net
Linkedin: https://es.linkedin.com/in/manuelcanga
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
---- En jue, 25 feb 2021 21:41:40 +0100 Nikita Popov nikita.ppv@gmail.com escribió ----
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
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
---- En jue, 25 feb 2021 21:41:40 +0100 Nikita Popov <
nikita.ppv@gmail.com> escribió ----On Thu, Feb 25, 2021 at 8:11 PM Manuel Canga php@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,
NikitaHi, 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 -- To unsubscribe, visit: https://www.php.net/unsub.php
Le 25/02/2021 à 21:41, Nikita Popov a écrit :
Could you please share the use case(s) you have in mind for this?
Regards,
Nikita
Hello,
I don't have a concrete example in mind right now, it happens I do need
this sometime.
It's mitigated by the fact I need it in most case when accessing class
reflection in order to introspect it and generate code or documentation
along or I do need to explode the whole class name to introspect
namespace parts.
Nevertheless, it still happens that I do magic with class names without
using the reflection, one use case that comes to mind is a class name to
logical name mapper I've written for converting PHP domain class names
to logical PHP-free message names for a bus in an heterogeneous infra
based upon a naming convention (using the namespaces as a basis for
string replacement).
--
Pierre