--
Manuel Canga

Zend Certified PHP Engineer 
Websites: https://manuelcanga.dev | https://trasweb.net
Linkedin: https://es.linkedin.com/in/manuelcanga




 ---- En jue, 25 feb 2021 23:12:23 +0100 David Gebler <davidgeb...@gmail.com> 
escribió ----
 > 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
 > 

Hi, David.

::namespace notation would be more semantic and faster.

Other example: In WordPress, classes use class-{{class-name}}.php notation for 
file of classes[1]. Example: class-wp-error.php

An autoloader for WordPress could be:

```
<?php

function autoload($fullClassName)
{
    $filePath = str_replace('\\', '//', $fullClassName::namespace );
    
    $className = ltrim(strrchr($fullClassName, '\\'), '\\');
    $className = strtolower( str_replace('_', '-', $className ) );

    require '{$filePath}/class-{$className}.php";
}

spl_autoload_register('autoload');
```


[1]: 
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
--
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