OOP translation of _ and \ to DIRECTORY_SEPARATOR - proposed tweak to standard
| From: | Greg Beaver | Date: | Thu, 11 Jun 2009 21:15:41 +0000 |
| Subject: | OOP translation of _ and \ to DIRECTORY_SEPARATOR - proposed tweak to standard | ||
| Groups: | php.standards | ||
| Request: | Send a blank email to standards-+get-64@lists.php.net to get a copy of this message | ||
Hi,
Since no one is choosing to respond to the technical question I raised
about the proposed standard, I'd like to ignore the folks who raised it
for the moment and start an actual discussion on the proposed naming
standard. I'll start with two separate threads, this one, and the one
titled "OOP naming - proposed tweak to standard"
Summary:
========
new: Change class name => path resolution so that _ and T_NS_SEPARATOR
are always translated to DIRECTORY_SEPARATOR.
old: _ is only translated to DIRECTORY_SEPARATOR if in the Class name
(not in the namespace name)
Details:
========
The standard proposed is:
vendor/package/Class/Name.php:
<?php
namespace vendor\package;
class Class_Name {}
?>
and:
vendor/package_name/ClassName.php:
<?php
namespace vendor\package_name;
class ClassName {}
?>
and finally:
vendor/package_name/subnamespace/another_subnamespace/Class/Name.php:
<?php
namespace vendor\package_name\subnamespace\another_subnamespace;
class Class_Name {}
?>
All of these result in a very complex resolution scheme for autoload:
1) split on namespace separator
2) find the first component that begins with a capital letter
3) split each component after the capital letter on underscore
4) concatenate with DIRECTORY_SEPARATOR
5) append ".php" and include
Before we even begin talking about the vendor\package\Classname, let's
talk about the above. 5 steps to locate a class is too complicated.
The underscore is not used to split a path if it is in the namespace,
but it is when it is in the class name.
Better is to simply specify either that both _ and \ are translated to
DIRECTORY_SEPARATOR always, or that ONLY \ is translated to directory
separator. The first results in this generic autoload:
function standard_autoload($class)
{
$file = str_replace(array('_', T_NS_SEPARATOR),
array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $class) . '.php';
include $file;
}
resolution scheme:
1) split on namespace separator and underscore
2) concatenate with DIRECTORY_SEPARATOR
3) append ".php" and include
This is far simpler, no? It allows straightforward eyeball resolution
of classname => class path as well
Thanks,
Greg