PHP编码规范:自动加载器特性强制性要求

下面描述了关于自动加载器特性强制性要求:

强制性

  • 一个完全标准的命名空间必须要有一下的格式结构\<Vendor Name>\(<Namespace>\)*<Class Name>
  • 命名空间必须有一个顶级的组织名称 (“Vendor Name”).
  • 命名空间中可以根据情况决定使用多少个子空间
  • 命名空间中的分隔符当从文件系统加载的时候将被映射为 DIRECTORY_SEPARATOR
  • 命名空间中的类名中的_没有特殊含义,也将被作为DIRECTORY_SEPARATOR对待.
  • 命名空间中的类名在从文件系统加载时文件名都需要以.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
  • \Zend\Acl => /path/to/project/lib/vendor/Zend/Acl.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_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php

以上是我们为实现无痛的自动加载特性设定的最低标准。你可以按照此标准实现一个SplClassLoader在PHP 5.3中去加载类。

实例

下面是一个函数实例简单展示如何使用上面建议的标准进行自动加载

<?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实现

下面的gist是一个SplClassLoader实例可以按照上面建议的自动加载特性来加载类。这也是我们当前推荐在PHP5.3中按照上述标准加载类的方式

16 thoughts on “PHP编码规范:自动加载器特性强制性要求

  1. Pingback: PHP编码规范:基本代码规范 | 郑州PHP.Net

  2. When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get 4 emails with the identical comment. Is there any manner you can remove me from that service? Thanks!

  3. Wonderful blog! Do you have any suggestions for aspiring writers?
    I’m planning to start my own site soon but I’m a little lost on everything.

    Would you suggest starting with a free platform like WordPress or
    go for a paid option? There are so many choices out there that
    I’m completely overwhelmed .. Any recommendations? Cheers!

  4. This is really interesting, You’re an excessively professional blogger.

    I’ve joined your feed and look forward to seeking
    extra of your excellent post. Additionally, I’ve shared
    your website in my social networks

  5. After checking out a few of the articles on your website,
    I honestly like your way of writing a blog. I saved it
    to my bookmark site list and will be checking back in the
    near future. Take a look at my web site too and tell me what you think.

  6. Hello, this weekend is good for me, for the reason that this occasion i
    am reading this fantastic informative post here at my home.

  7. Excellent blog here! Additionally your site so much
    up fast! What host are you the usage of? Can I am getting your affiliate hyperlink for your host?

    I desire my website loaded up as fast as yours lol

  8. Definitely consider that that you stated. Your favourite justification seemed to be on the
    internet the easiest factor to be mindful of. I say to you, I
    certainly get irked while people think about worries that they plainly don’t
    know about. You controlled to hit the nail upon the highest and
    also outlined out the entire thing without having side effect , folks could take a signal.
    Will likely be back to get more. Thanks

  9. Always make sure to wear protective devices prior to
    servicing them as they are chemically reactive. Use safety glasses and also gloves
    for your security. Get rid of the battery from the lorry before testing it.
    Use a basic wrench to remove it from the bolts of the battery
    owner. Put it on a surface area that is not chemically responsive.

    The following step is the preparation of a service required
    for carrying out the operation. It is made by mixing
    4 ounces of Epsom salt with every 1 quarter of water. Never ever use tap water
    as it can be hazardous to the battery cells.
    Always make use of distilled water implied for usage in batteries.
    As soon as the option is prepared, open up the caps of the cells
    and drain pipes the water consisted of in them. After that you need to load
    then vacant cell with the prepared service of Epsom salt as
    well as distilled water. Use a funnel to make the procedure very easy.
    After filling it shake the cells well for even circulation of the option inside the
    battery cells.

    Following step is to recharge the battery. A 3 phase lead acid battery charger can be utilized for billing it.
    Make sure the battery charger remains in OFF setting before you connect the battery to the charger.
    After effectively connecting it transform the charger ON
    as well as leave it over night for billing.

Leave a Reply to cheap nfl jerseys Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>