简体   繁体   English

PHP重新定义常量,而无需运行工具包或声明命名空间的其他方法

[英]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. 以下内容描述了自动装载机互操作性必须遵守的强制性要求。

Mandatory 强制性的

  • A fully-qualified namespace and class must have the following structure \\<Vendor Name>\\(<Namespace>\\)*<Class Name> 完全限定的名称空间和类必须具有以下结构\\<Vendor Name>\\(<Namespace>\\)*<Class Name>
  • Each namespace must have a top-level namespace ("Vendor Name"). 每个名称空间必须具有顶级名称空间(“供应商名称”)。
  • Each namespace can have as many sub-namespaces as it wishes. 每个名称空间可以根据需要具有任意数量的子名称空间。
  • Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system. 从文件系统加载时,每个名称空间分隔符都会转换为DIRECTORY_SEPARATOR
  • Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR . 类别名称中的每个_字符都将转换为DIRECTORY_SEPARATOR The _ character has no special meaning in the namespace. _字符在名称空间中没有特殊含义。
  • The fully-qualified namespace and class is suffixed with .php when loading from the file system. 从文件系统加载时,标准的名称空间和类的后缀为.php
  • Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case. 供应商名称,名称空间和类名称中的字母字符可以是小写和大写的任意组合。

Examples 例子

  • \\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

Underscores in Namespaces and Class Names 命名空间和类名中的下划线

  • \\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类。

Example Implementation 示例实施

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;
}

SplClassLoader Implementation SplClassLoader实现

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM