成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

PSR-4:自動(dòng)加載

ZweiZhao / 1233人閱讀

摘要:概述這份聲明了關(guān)于從文件路徑自動(dòng)加載類的規(guī)范。當(dāng)根據(jù)完全限定類名加載對(duì)應(yīng)的文件時(shí)由最開始的命名空間開始,連續(xù)的一個(gè)或多個(gè)命名空間組成的序列,不包括最前面的命名空間分隔符,在這個(gè)完全限定類名中這個(gè)序列稱為命名空間前綴,對(duì)應(yīng)了至少一個(gè)基礎(chǔ)目錄。

PSR-4:自動(dòng)加載

翻譯:薛粲
授權(quán)許可:CC BY-NC 4.0

這份文檔是《PSR-4: Autoloader》的非官方譯文。

英文原文使用的關(guān)鍵詞 "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", 以及 "OPTIONAL" 遵循 RFC 2119 的描述。譯文中根據(jù)上下文可能會(huì)使用不同的詞匯來(lái)對(duì)應(yīng)這些關(guān)鍵詞,并加粗顯示。

1. 概述

這份 PSR 聲明了關(guān)于從文件路徑自動(dòng)加載(autoloading)類的規(guī)范。它具有完整的互用性,可以結(jié)合任何其它關(guān)于自動(dòng)加載機(jī)制的規(guī)范使用,包括 PSR-0。這份 PSR 還描述了在何處保存文件以便可以根據(jù)這份規(guī)范實(shí)現(xiàn)自動(dòng)加載。

2. 規(guī)范

術(shù)語(yǔ)“類”指代類、接口、trait 以及其它類似的結(jié)構(gòu)。

一個(gè)完全限定(fully qualified)類名形如:()*

完全限定類名必須包含一個(gè)頂級(jí)命名空間名,這也被稱為“開發(fā)商命名空間”。

完全限定類名可以包含一個(gè)或多個(gè)子命名空間名稱。

完全限定類名必須以一個(gè)類名結(jié)束。

完全限定類名各部分中的下劃線沒(méi)有任何特殊的含義。

完全限定類名中的字母可以使用任意大小寫組合。

所有類名必須以大小寫敏感的方式被引用。

當(dāng)根據(jù)完全限定類名加載對(duì)應(yīng)的文件時(shí):

由最開始的命名空間開始,連續(xù)的一個(gè)或多個(gè)命名空間組成的序列,不包括最前面的命名空間分隔符,在這個(gè)完全限定類名中(這個(gè)序列稱為“命名空間前綴,namespace prefix”)對(duì)應(yīng)了至少一個(gè)“基礎(chǔ)目錄”。

“命名空間前綴”之后相鄰的子命名空間,對(duì)應(yīng)“基礎(chǔ)目錄”中的子目錄,命名空間分隔符對(duì)應(yīng)目錄分隔符。子目錄名必須與子命名空間名大小寫匹配。

結(jié)尾的類名對(duì)應(yīng)一個(gè)以 .php 最為后綴的文件名。文件名必須與類名大小寫匹配。

自動(dòng)加載機(jī)制的具體實(shí)現(xiàn)不得拋出異常,不得產(chǎn)生任何等級(jí)的錯(cuò)誤,且不應(yīng)該有返回值。

3. 示例

下表列出了一些文件路徑及其相對(duì)應(yīng)的完全限定類名、命名空間前綴和基礎(chǔ)目錄。

完全限定類名 命名空間前綴 基礎(chǔ)目錄 文件路徑
AcmeLogWriterFile_Writer AcmeLogWriter ./acme-log-writer/lib/ ./acme-log-writer/lib/File_Writer.php
AuraWebResponseStatus AuraWeb /path/to/aura-web/src/ /path/to/aura-web/src/Response/Status.php
SymfonyCoreRequest SymfonyCore ./vendor/Symfony/Core/ ./vendor/Symfony/Core/Request.php
endAcl Zend /usr/includes/Zend/ /usr/includes/Zend/Acl.php

PSR-4 提供遵循本規(guī)范的自動(dòng)加載機(jī)制實(shí)現(xiàn)范例。這些范例不得被當(dāng)做本規(guī)范的一部分,并可能隨時(shí)修改。


實(shí)現(xiàn)范例

下面的示例提供了遵循 PSR-4 的參考實(shí)現(xiàn)。

使用閉包的范例

使用類的范例

下面基于類的實(shí)現(xiàn)范例可以處理多個(gè)命名空間:

register();
 *      
 *      // register the base directories for the namespace prefix
 *      $loader->addNamespace("FooBar", "/path/to/packages/foo-bar/src");
 *      $loader->addNamespace("FooBar", "/path/to/packages/foo-bar/tests");
 * 
 * 下面的代碼將會(huì)嘗試從文件
 * /path/to/packages/foo-bar/src/Qux/Quux.php
 * 加載類 FooBarQuxQuux:
 * 
 *      prefixes[$prefix]) === false) {
            $this->prefixes[$prefix] = array();
        }

        // retain the base directory for the namespace prefix
        if ($prepend) {
            array_unshift($this->prefixes[$prefix], $base_dir);
        } else {
            array_push($this->prefixes[$prefix], $base_dir);
        }
    }

    /**
     * Loads the class file for a given class name.
     *
     * @param string $class The fully-qualified class name.
     * @return mixed The mapped file name on success, or boolean false on
     * failure.
     */
    public function loadClass($class)
    {
        // the current namespace prefix
        $prefix = $class;

        // work backwards through the namespace names of the fully-qualified
        // class name to find a mapped file name
        while (false !== $pos = strrpos($prefix, "")) {

            // retain the trailing namespace separator in the prefix
            $prefix = substr($class, 0, $pos + 1);

            // the rest is the relative class name
            $relative_class = substr($class, $pos + 1);

            // try to load a mapped file for the prefix and relative class
            $mapped_file = $this->loadMappedFile($prefix, $relative_class);
            if ($mapped_file) {
                return $mapped_file;
            }

            // remove the trailing namespace separator for the next iteration
            // of strrpos()
            $prefix = rtrim($prefix, "");   
        }

        // never found a mapped file
        return false;
    }

    /**
     * Load the mapped file for a namespace prefix and relative class.
     * 
     * @param string $prefix The namespace prefix.
     * @param string $relative_class The relative class name.
     * @return mixed Boolean false if no mapped file can be loaded, or the
     * name of the mapped file that was loaded.
     */
    protected function loadMappedFile($prefix, $relative_class)
    {
        // are there any base directories for this namespace prefix?
        if (isset($this->prefixes[$prefix]) === false) {
            return false;
        }

        // look through base directories for this namespace prefix
        foreach ($this->prefixes[$prefix] as $base_dir) {

            // replace the namespace prefix with the base directory,
            // replace namespace separators with directory separators
            // in the relative class name, append with .php
            $file = $base_dir
                  . str_replace("", "/", $relative_class)
                  . ".php";

            // if the mapped file exists, require it
            if ($this->requireFile($file)) {
                // yes, we"re done
                return $file;
            }
        }

        // never found it
        return false;
    }

    /**
     * If a file exists, require it from the file system.
     * 
     * @param string $file The file to require.
     * @return bool True if the file exists, false if not.
     */
    protected function requireFile($file)
    {
        if (file_exists($file)) {
            require $file;
            return true;
        }
        return false;
    }
}
單元測(cè)試

下面的范例是對(duì)上面的類加載機(jī)制的一種單元測(cè)試實(shí)現(xiàn)方案:

files = $files;
    }

    protected function requireFile($file)
    {
        return in_array($file, $this->files);
    }
}

class Psr4AutoloaderClassTest extends PHPUnit_Framework_TestCase
{
    protected $loader;

    protected function setUp()
    {
        $this->loader = new MockPsr4AutoloaderClass;

        $this->loader->setFiles(array(
            "/vendor/foo.bar/src/ClassName.php",
            "/vendor/foo.bar/src/DoomClassName.php",
            "/vendor/foo.bar/tests/ClassNameTest.php",
            "/vendor/foo.bardoom/src/ClassName.php",
            "/vendor/foo.bar.baz.dib/src/ClassName.php",
            "/vendor/foo.bar.baz.dib.zim.gir/src/ClassName.php",
        ));

        $this->loader->addNamespace(
            "FooBar",
            "/vendor/foo.bar/src"
        );

        $this->loader->addNamespace(
            "FooBar",
            "/vendor/foo.bar/tests"
        );

        $this->loader->addNamespace(
            "FooBarDoom",
            "/vendor/foo.bardoom/src"
        );

        $this->loader->addNamespace(
            "FooBarBazDib",
            "/vendor/foo.bar.baz.dib/src"
        );

        $this->loader->addNamespace(
            "FooBarBazDibimGir",
            "/vendor/foo.bar.baz.dib.zim.gir/src"
        );
    }

    public function testExistingFile()
    {
        $actual = $this->loader->loadClass("FooBarClassName");
        $expect = "/vendor/foo.bar/src/ClassName.php";
        $this->assertSame($expect, $actual);

        $actual = $this->loader->loadClass("FooBarClassNameTest");
        $expect = "/vendor/foo.bar/tests/ClassNameTest.php";
        $this->assertSame($expect, $actual);
    }

    public function testMissingFile()
    {
        $actual = $this->loader->loadClass("No_VendorNo_PackageNoClass");
        $this->assertFalse($actual);
    }

    public function testDeepFile()
    {
        $actual = $this->loader->loadClass("FooBarBazDibimGirClassName");
        $expect = "/vendor/foo.bar.baz.dib.zim.gir/src/ClassName.php";
        $this->assertSame($expect, $actual);
    }

    public function testConfusion()
    {
        $actual = $this->loader->loadClass("FooBarDoomClassName");
        $expect = "/vendor/foo.bar/src/DoomClassName.php";
        $this->assertSame($expect, $actual);

        $actual = $this->loader->loadClass("FooBarDoomClassName");
        $expect = "/vendor/foo.bardoom/src/ClassName.php";
        $this->assertSame($expect, $actual);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/21466.html

相關(guān)文章

  • PHP自動(dòng)加載功能原理解析

    摘要:前言在開始之前,歡迎關(guān)注我自己的博客這篇文章是對(duì)自動(dòng)加載功能的一個(gè)總結(jié),內(nèi)容涉及的自動(dòng)加載功能的命名空間的與標(biāo)準(zhǔn)等內(nèi)容。要實(shí)現(xiàn)第一步,第二步的功能,必須在開發(fā)時(shí)約定類名與磁盤文件的映射方法,只有這樣我們才能根據(jù)類名找到它對(duì)應(yīng)的磁盤文件。 前言 在開始之前,歡迎關(guān)注我自己的博客:www.leoyang90.cn 這篇文章是對(duì)PHP自動(dòng)加載功能的一個(gè)總結(jié),內(nèi)容涉及PHP的自動(dòng)加載功能、P...

    Imfan 評(píng)論0 收藏0
  • composer 自動(dòng)加載

    摘要:自動(dòng)加載是指在代碼中,不需要顯式地使用文件路徑將類庫(kù)文件包含進(jìn)來(lái),便可使用該文件中定義的類庫(kù)。在里是這樣進(jìn)行配置的按照的規(guī)則,當(dāng)試圖自動(dòng)加載這個(gè)時(shí),會(huì)去尋找這個(gè)文件。最后,只要在項(xiàng)目中你所需要的所有類庫(kù)都會(huì)在適當(dāng)?shù)臅r(shí)候自動(dòng)載入。 Composer是PHP中用來(lái)管理依賴(dependency)關(guān)系的工具。你可以在自己的項(xiàng)目中聲明所依賴的外部工具庫(kù)(libraries),Composer...

    alighters 評(píng)論0 收藏0
  • PHP包管理工具--Composer自動(dòng)加載

    摘要:接觸過(guò)的同學(xué)都知道使用作為項(xiàng)目的包管理工具但是并不是獨(dú)有的是的包管理工具這兩者的關(guān)系就像于于一樣但是發(fā)現(xiàn)真正項(xiàng)目中使用還是比較少的所以這里這里寫一遍文章介紹的使用幫助那些對(duì)于還是有點(diǎn)模糊的同學(xué)此文跟沒(méi)有任何聯(lián)系安裝的方式就不講了具體安裝方式 接觸過(guò)Laravel的同學(xué)都知道,Laravel使用Composer作為項(xiàng)目的包管理工具.但是Composer并不是Laravel獨(dú)有的,Comp...

    xiaoqibTn 評(píng)論0 收藏0
  • Yii2中的代碼自動(dòng)加載機(jī)制

    摘要:中是如何實(shí)現(xiàn)代碼的自動(dòng)加載的入口腳本的以下兩行代碼其中的作用注冊(cè)為自動(dòng)加載函數(shù)。這個(gè)負(fù)責(zé)引入了一個(gè)類中的,隨后立即解除注冊(cè)。注冊(cè)中的為自動(dòng)加載函數(shù),并利用配置文件即目錄下的文件對(duì)這個(gè)自動(dòng)加載函數(shù)進(jìn)行了初始化。 1.基本知識(shí) Include與require 的作用: 當(dāng)一個(gè)文件被包含時(shí),其中所包含的代碼繼承了 include 所在行的變量范圍。從該處開始,調(diào)用文件在該行處可用的任何...

    Jaden 評(píng)論0 收藏0
  • PSR-4——新鮮出爐的PHP規(guī)范

    摘要:制定的規(guī)范,簡(jiǎn)稱,是開發(fā)的事實(shí)標(biāo)準(zhǔn)。原本有四個(gè)規(guī)范,分別是自動(dòng)加載基本代碼規(guī)范代碼樣式日志接口年底,新出了第個(gè)規(guī)范。區(qū)別在于的規(guī)范比較干凈,去除了兼容以前版本的內(nèi)容,有一點(diǎn)升級(jí)版的感覺(jué)。 FIG制定的PHP規(guī)范,簡(jiǎn)稱PSR,是PHP開發(fā)的事實(shí)標(biāo)準(zhǔn)。 PSR原本有四個(gè)規(guī)范,分別是: PSR-0 自動(dòng)加載 PSR-1 基本代碼規(guī)范 PSR-2 代碼樣式 PSR-3 日志接口 20...

    Fundebug 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<