[英]PHP redefine constant without runkit or another way to declare a namespace
I am doing a framework similar to CodeIgniter but with namespaces. 我正在做一个类似于CodeIgniter的框架,但带有名称空间。 I don't want use runkit because many hosting providers don't have the extension.
我不想使用runkit,因为许多托管服务提供商没有扩展名。
The Namespaces are based on paths to the file class, if I who load the file class too I know the namespace has the class but I don´t know what sintax to use to declare the namespace. 命名空间基于文件类的路径,如果我也加载文件类,则我知道命名空间具有该类,但是我不知道该使用什么正弦表达式来声明该命名空间。
I apologize for my english. 我为我的英语道歉。 I will try to explain me using a example:
我将尝试使用一个例子来解释我:
namespace \FlexIgniter\Core
class Load {
public function loadClass($name, $ns) {
runkit_constant_redefine("NAMESPACE_AUTO", $ns);
$path = $this->class2path($class);
include_once($path);
}
}
The file to load: 要加载的文件:
namespace NAMESPACE_AUTO;
class ToLoad {
// do some stuff...
}
Thanks. 谢谢。
PS: https://github.com/Localnet/FlexIgniter PS: https : //github.com/Localnet/FlexIgniter
Here are the requirements for PSR-0 : 这是PSR-0的要求 :
The following describes the mandatory requirements that must be adhered to for autoloader interoperability. 以下内容描述了自动装载机互操作性必须遵守的强制性要求。
\\<Vendor Name>\\(<Namespace>\\)*<Class Name>
\\<Vendor Name>\\(<Namespace>\\)*<Class Name>
DIRECTORY_SEPARATOR
when loading from the file system. DIRECTORY_SEPARATOR
。 _
character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR
. _
字符都将转换为DIRECTORY_SEPARATOR
。 The _
character has no special meaning in the namespace. _
字符在名称空间中没有特殊含义。 .php
when loading from the file system. .php
。 \\Doctrine\\Common\\IsolatedClassLoader
=> /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\\Doctrine\\Common\\IsolatedClassLoader
=> /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
\\Symfony\\Core\\Request
=> /path/to/project/lib/vendor/Symfony/Core/Request.php
\\Symfony\\Core\\Request
=> /path/to/project/lib/vendor/Symfony/Core/Request.php
\\Zend\\Acl
=> /path/to/project/lib/vendor/Zend/Acl.php
\\Zend\\Acl
=> /path/to/project/lib/vendor/Zend/Acl.php
\\Zend\\Mail\\Message
=> /path/to/project/lib/vendor/Zend/Mail/Message.php
\\Zend\\Mail\\Message
=> /path/to/project/lib/vendor/Zend/Mail/Message.php
\\namespace\\package\\Class_Name
=> /path/to/project/lib/vendor/namespace/package/Class/Name.php
\\namespace\\package\\Class_Name
=> /path/to/project/lib/vendor/namespace/package/Class/Name.php
\\namespace\\package_name\\Class_Name
=> /path/to/project/lib/vendor/namespace/package_name/Class/Name.php
\\namespace\\package_name\\Class_Name
=> /path/to/project/lib/vendor/namespace/package_name/Class/Name.php
The standards we set here should be the lowest common denominator for painless autoloader interoperability. 我们在此处设置的标准应该是实现无痛自动装带器互操作性的最低标准。 You can test that you are following these standards by utilizing this sample SplClassLoader implementation which is able to load PHP 5.3 classes.
您可以利用此示例SplClassLoader实现来测试您是否遵守这些标准,该实现能够加载PHP 5.3类。
Below is an example function to simply demonstrate how the above proposed standards are autoloaded. 下面是一个示例函数,用于简单演示如何自动加载上述建议的标准。
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
The following gist is a sample SplClassLoader implementation that can load your classes if you follow the autoloader interoperability standards proposed above. 以下要点是一个示例SplClassLoader实现,如果您遵循上面建议的自动加载程序互操作性标准,则可以加载您的类。 It is the current recommended way to load PHP 5.3 classes that follow these standards.
这是当前建议的加载遵循这些标准的PHP 5.3类的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.