亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
PSR-0 - 自動(dòng)加載標(biāo)準(zhǔn)
PSR-1 - 基本編碼標(biāo)準(zhǔn)
命名約定
代碼約定:
PSR-2 - 高級(jí)編碼標(biāo)準(zhǔn)
PHPCS - PHP 代碼嗅探器
結(jié)論
Home Backend Development PHP Tutorial New title: Obvious PSR!

New title: Obvious PSR!

Aug 27, 2023 pm 09:41 PM
psr obvious

在 Nettuts+ 的上一課中,您了解了 PSR;但是,該文章沒(méi)有詳細(xì)說(shuō)明將該編碼風(fēng)格集成到項(xiàng)目中的過(guò)程。讓我們解決這個(gè)問(wèn)題!

注意:本文假設(shè)您已閱讀 PSR-Huh?,并了解 PSR 指的是什么。讓我們從第一個(gè)標(biāo)準(zhǔn)開(kāi)始:PSR-0。


PSR-0 - 自動(dòng)加載標(biāo)準(zhǔn)

PHPCS 插件是我用過(guò)的最有用的工具。

過(guò)去,我們通過(guò)以下兩種方式之一包含 PHP 文件:

  • 在每個(gè)文件的頂部使用大量包含語(yǔ)句。
  • 列出單個(gè)文件中的所有包含內(nèi)容,并將該單個(gè)文件包含在您的項(xiàng)目中。

這兩種方法各有利弊,但是,我認(rèn)為我們都同意這兩種方法都不是最佳或現(xiàn)代的解決方案。 PHP5引入了根據(jù)類名自動(dòng)加載文件的概念;因此,PSR-0 旨在保持文件名一致。

命名空間與文件名或自動(dòng)加載無(wú)關(guān);從技術(shù)上講,您可以在同一文件中聲明不同的名稱空間。例如,下面的代碼是完全有效的。

<?php
namespace Nettuts;

Class Hello
{
    public function __construct()
    {
        echo "Nettuts+";
    }
}

namespace Gabriel;

Class Hello
{
    public function __construct()
    {
        echo "Gabriel";
    }
}

$h = new \Nettuts\Hello();
$h = new \Gabriel\Hello();

這個(gè)文件中有兩個(gè) Hello 類,但它們駐留在不同的命名空間中。此代碼的最后兩行在各自的命名空間上實(shí)例化 Hello() 類。第一個(gè)輸出“Nettuts+”,而第二個(gè)輸出“Gabriel”。命名空間允許您區(qū)分具有相同名稱的兩個(gè)類,就像您可能習(xí)慣于桌面上的文件夾一樣。 PSR-0 標(biāo)準(zhǔn)簡(jiǎn)單地利用了命名空間的優(yōu)勢(shì),使自動(dòng)加載類變得容易。通過(guò)一致地命名文件,您可以創(chuàng)建一個(gè)自動(dòng)查找必要文件的函數(shù)。

要符合 PSR-1 標(biāo)準(zhǔn),您還必須遵循 PSR-0。

請(qǐng)務(wù)必閱讀完整的標(biāo)準(zhǔn),但總結(jié)一下:

  • 每個(gè)類都必須以項(xiàng)目(或創(chuàng)建者)的名稱命名。
  • 類名稱中的下劃線應(yīng)轉(zhuǎn)換為目錄分隔符。
  • 文件必須具有 .php 擴(kuò)展名。

例如,類引用:

\Nettuts\Database\SQL_Postgres

如果遵循 PSR-0,應(yīng)轉(zhuǎn)換為此路徑:

./Nettuts/Database/SQL/Postgres.php

我們?nèi)绾螌?shí)現(xiàn)此功能?最明顯的解決方案是使用 Composer,它附帶了符合 PSR-0 標(biāo)準(zhǔn)的自動(dòng)加載器。如果您在項(xiàng)目中使用 Composer(您應(yīng)該這樣做),那么請(qǐng)選擇它的自動(dòng)加載器,而不是編寫(xiě)自己的自動(dòng)加載器。

符合 PSR-0 的加載程序允許您指定基本路徑,通知加載程序首先查看哪個(gè)目錄。首先,創(chuàng)建一個(gè)簡(jiǎn)單的 composer.json 文件,其中包含以下 JSON:

{
    "autoload": {
        "psr-0": {
            "Nettuts": "./",
            "Gmanricks": "vendor/"
        }
    }
}

這個(gè) JSON 文件告訴 Composer 我們要使用 PSR-0 標(biāo)準(zhǔn)自動(dòng)加載所有以當(dāng)前目錄(根文件夾)為基本路徑的 Nettuts 命名空間文件。我們還希望使用 Gmanricks 命名空間自動(dòng)加載相對(duì)于 vendor 文件夾的所有類(例如 ./vendor/Gmanricks/ClassName)。

現(xiàn)在,輸入“composer install”以生成自動(dòng)加載類,或在后續(xù)編輯中輸入“composer dump-autoload”以重新生成自動(dòng)加載類。另外,不要忘記在項(xiàng)目早期的某個(gè)地方需要自動(dòng)加載器。

<?php

require 'vendor/autoload.php';

Composer 是您的最佳選擇,但在某些情況下您可能需要一個(gè)小型、簡(jiǎn)單的自動(dòng)加載器。 PHP-FIG 提供了一個(gè)可供您使用的示例自動(dòng)加載器:

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

需要注意的是,此加載器嘗試加載當(dāng)前目錄中使用 PSR 標(biāo)準(zhǔn)的所有類。

現(xiàn)在我們已經(jīng)成功地自動(dòng)加載類了,讓我們繼續(xù)討論下一個(gè)標(biāo)準(zhǔn):基本編碼標(biāo)準(zhǔn)。


PSR-1 - 基本編碼標(biāo)準(zhǔn)

PSR-1 定義了通用編碼指南,可以分為兩部分。

命名約定

命名空間允許您區(qū)分具有相同名稱的兩個(gè)類。

與任何編程語(yǔ)言一樣,遵循命名約定最終會(huì)使您的代碼更易于閱讀和維護(hù)。以下是一些需要遵循的規(guī)則:

  • 類名稱使用PascalCase。
  • 方法名稱應(yīng)采用駝峰命名法。
  • 常量要求全部大寫(xiě)字母,并用下劃線分隔每個(gè)單詞(例如 CONSTANT_VARIABLE)。

代碼約定:

它不僅僅是命名約定;還請(qǐng)遵循以下準(zhǔn)則:

  • 僅在代碼中使用 <?php<?=。不要在類中關(guān)閉 PHP。
  • 文件應(yīng)該聲明符號(hào)或使用它們。
  • PHP 代碼的文件必須采用無(wú) BOM 的 UTF-8 格式

其中大部分都是不言自明的,但中間的約定有點(diǎn)令人困惑。它本質(zhì)上規(guī)定任何聲明,無(wú)論是函數(shù)、類等,都應(yīng)該分離到它們自己的文件中。這不僅促進(jìn)了代碼重用和分離等最佳實(shí)踐,而且使您的代碼保持整潔。

值得一提的是,每個(gè) PSR 標(biāo)準(zhǔn)都建立在之前的 PSR 標(biāo)準(zhǔn)之上。因此,要符合 PSR-1,您還必須遵循 PSR-0。通過(guò)遵循這兩個(gè)標(biāo)準(zhǔn),您的代碼將被正確命名并自動(dòng)加載。確實(shí)沒(méi)有理由不關(guān)注他們。

是的,一些開(kāi)發(fā)人員抱怨 PSR 并更喜歡遵循其他約定,但通過(guò)遵循此標(biāo)準(zhǔn),您可以與所有人共享代碼,而不必?fù)?dān)心其一致性。話雖如此,沒(méi)有人強(qiáng)迫你這么做。這只是一個(gè)推薦指南。

下一個(gè)標(biāo)準(zhǔn) PSR-2 深入探討了如何構(gòu)建代碼的細(xì)節(jié)。


PSR-2 - 高級(jí)編碼標(biāo)準(zhǔn)

PSR-2 深入探討了如何構(gòu)建代碼的細(xì)節(jié)。

接下來(lái),我們來(lái)看看 PHP 開(kāi)發(fā)人員最難解決的一個(gè)標(biāo)準(zhǔn):事實(shí)上,這就是我選擇寫(xiě)這篇文章的原因。

PSR-2 定義了許多規(guī)則,其中許多規(guī)則如下:

  • 應(yīng)使用四個(gè)空格而不是制表符。
  • 理想的行長(zhǎng)度應(yīng)低于 80 個(gè)字符,但所有行都應(yīng)施加 120 個(gè)字符的軟限制。
  • namespaceuse 聲明下應(yīng)有一個(gè)空行。
  • 方法或類的左大括號(hào)必須獨(dú)占一行。
  • 方法或類的右大括號(hào)必須緊接在正文之后。
  • 所有屬性和方法都需要可見(jiàn)性級(jí)別。
  • abstract”/“final”關(guān)鍵字應(yīng)出現(xiàn)在可見(jiàn)性之前,而“static”則出現(xiàn)在可見(jiàn)性之后。
  • 控制結(jié)構(gòu)關(guān)鍵字后必須跟一個(gè)空格。
  • 控制語(yǔ)句的左大括號(hào)應(yīng)與語(yǔ)句出現(xiàn)在同一行。

請(qǐng)務(wù)必查看整個(gè)規(guī)范以獲得完整的概述。

PSR-2 與 PSR-1(和 PSR-0)一樣重要。它的目的是使代碼易于閱讀和維護(hù)。但是,正如他們所說(shuō),“細(xì)節(jié)決定成敗。”有很多細(xì)節(jié)需要記住,如果您的編程習(xí)慣與標(biāo)準(zhǔn)定義的不同,那么記住這些細(xì)節(jié)可能會(huì)很困難。值得慶幸的是,如果您同意,有一些工具可以幫助您遵守 PSR-0、PSR-1 和 PSR-2。也許最好的工具是 Sublime Text 插件 PHPCS。


PHPCS - PHP 代碼嗅探器

PHPCS 插件是我用過(guò)的最有幫助的工具,對(duì)于讓代碼成型。它不僅可以讓您確保您的代碼遵循 PSR 標(biāo)準(zhǔn),還可以使用 PHP 的 linter 檢查語(yǔ)法錯(cuò)誤。這非常節(jié)省時(shí)間;在瀏覽器中測(cè)試代碼時(shí),您不必再擔(dān)心語(yǔ)法錯(cuò)誤。

通過(guò) Sublime Package Control(稱為 Phpcs)安裝包,或者使用 Git,使用以下命令安裝包:

cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
git clone git://github.com/benmatselby/sublime-phpcs.git Phpcs

這將安裝插件,但您需要一些依賴項(xiàng)才能配置 PHPCS。再次強(qiáng)調(diào),最簡(jiǎn)單的安裝方法是使用 Composer。瀏覽到您選擇的目錄并使用以下 JSON 創(chuàng)建 composer.json 文件:

{
    "name": "Nettuts PHPCS Demo",
    "require": {
        "squizlabs/php_codesniffer": "*",
        "fabpot/php-cs-fixer": "*",
        "phpmd/phpmd": "*"
    }
}

這會(huì)將三個(gè)依賴項(xiàng)安裝到當(dāng)前文件夾中。打開(kāi)一個(gè)終端窗口到您的安裝位置并輸入 composer install,它將下載必要的軟件包。

現(xiàn)在您可以在 Sublime Text 中配置插件了。導(dǎo)航到“首選項(xiàng)”>“包設(shè)置”>“PHP 代碼嗅探器”>“設(shè)置 - 用戶”。

New title: Obvious PSR!

插件需要知道三個(gè)依賴項(xiàng)所在的位置,以及我們希望代碼遵守的標(biāo)準(zhǔn):

{
    "phpcs_additional_args": {
        "--standard": "PSR2",
        "-n": ""
    },
    "phpcs_executable_path": "DEPENDENCY_PATH/vendor/bin/phpcs",
    "phpmd_executable_path": "DEPENDENCY_PATH/vendor/bin/phpmd",
    "php_cs_fixer_executable_path": "DEPENDENCY_PATH/vendor/bin/php-cs-fixer"
}

這些設(shè)置告知 PHPCS 我們希望遵守 PSR2 標(biāo)準(zhǔn)并提供每個(gè)依賴項(xiàng)的路徑。不要忘記將 DEPENDENCY_PATH 替換為您的實(shí)際路徑。

重新啟動(dòng) Sublime,代碼嗅探器將在您保存 PHP 文件時(shí)掃描您的代碼。

New title: Obvious PSR!

在編輯器中右鍵單擊還將列出幾個(gè)新選項(xiàng),例如清除錯(cuò)誤標(biāo)記和嘗試修復(fù)非標(biāo)準(zhǔn)問(wèn)題。但是,考慮到本文的目的是讓您習(xí)慣該標(biāo)準(zhǔn),我建議手動(dòng)修復(fù)您的代碼并避免使用自動(dòng)修復(fù)程序功能。


結(jié)論

創(chuàng)建 PSR 標(biāo)準(zhǔn)是為了使代碼可以輕松地在項(xiàng)目之間重用,而無(wú)需犧牲代碼風(fēng)格的一致性。雖然一開(kāi)始可能會(huì)感到不知所措,但您可以使用本文中的想法和工具來(lái)幫助您完成過(guò)渡。

最后重申一次:沒(méi)有人強(qiáng)迫您改變 PHP 編碼的方式。它只是一個(gè)指南,最初是為了框架互操作性。也就是說(shuō),在 Nettuts+,我們認(rèn)為這是值得遵循的最佳實(shí)踐?,F(xiàn)在你自己拿主意吧!如果您有任何疑問(wèn),請(qǐng)?jiān)谙旅媛?tīng)聽(tīng)!

The above is the detailed content of New title: Obvious PSR!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Application and promotion of PSR2 and PSR4 specifications in Lumen microframework Application and promotion of PSR2 and PSR4 specifications in Lumen microframework Oct 15, 2023 am 11:21 AM

Application and promotion of PSR2 and PSR4 specifications in the Lumen microframework Introduction: With the widespread application and development of the PHP language, code specifications have become an important aspect to maintain code quality and readability. PHPFIG (PHPFIG, PHPFrameworkInteropGroup) has created a series of best practice specifications (PSR, PHPStandardsRecommendations) on PHP development, among which PSR2 and PSR

Promotion and practice of PSR2 and PSR4 specifications in CodeIgniter development Promotion and practice of PSR2 and PSR4 specifications in CodeIgniter development Oct 15, 2023 am 11:25 AM

Promotion and practice of PSR2 and PSR4 specifications in CodeIgniter development Introduction: In the CodeIgniter development process, following coding specifications is an important aspect. Among them, the PSR2 and PSR4 specifications are widely adopted standards in the PHP community, helping to unify coding styles and improve team collaboration efficiency. This article will introduce how to promote and practice these two specifications in the CodeIgniter project, and provide specific code examples. 1. What is PSR2 and PSR4 specifications PSR2

Code specification checking tool based on PHP's PSR2 and PSR4 specifications Code specification checking tool based on PHP's PSR2 and PSR4 specifications Oct 15, 2023 pm 05:33 PM

Code specification checking tool based on PHP's PSR-2 and PSR-4 specifications: implementation and examples Introduction: In the software development process, good code specifications are an important factor in ensuring program quality and maintainability. In order to help developers follow PHP code specifications, PHP-FIG (PHPFrameworkInteropGroup) proposed the PSR (PHPStandardsRecommendations) specification series. Among them, PSR-2 mainly defines

Application and promotion of PSR2 and PSR4 specifications in Fat-Free framework Application and promotion of PSR2 and PSR4 specifications in Fat-Free framework Oct 15, 2023 am 10:24 AM

Application and promotion of PSR2 and PSR4 specifications in the Fat-Free framework With the continuous development of the PHP language and the expansion of its application scope, many developers realize that writing standardized code is of great significance to the long-term maintenance of the project and team collaboration. To this end, PHPFIG (PHP Developers Interest Group) has developed a series of coding specifications, including PSR2 and PSR4 specifications. This article will focus on the application and promotion of these two specifications in the Fat-Free framework, and give corresponding code examples. first

New title: Obvious PSR! New title: Obvious PSR! Aug 27, 2023 pm 09:41 PM

In the previous lesson on Nettuts+, you learned about PSR; however, the article did not detail the process of integrating this coding style into your project. Let's fix this problem! NOTE: This article assumes you have read PSR-Huh? and understand what PSR refers to. Let's start with the first standard: PSR-0. PSR-0 - Autoload Standard PHPCS plugin is the most useful tool I have ever used. In the past, we included PHP files in one of two ways: using a lot of include statements at the top of each file. List all includes in a single file and include that single file in your project. There are pros and cons to both approaches, however, I think we can all agree that neither is the best or modern solution

Impact of PHP PSR2 and PSR4 specifications on code quality Impact of PHP PSR2 and PSR4 specifications on code quality Oct 15, 2023 pm 02:21 PM

The impact of PHPPSR2 and PSR4 specifications on code quality requires specific code examples Introduction: In the software development process, both individuals and teams hope to write high-quality code. PHPPSR (PHPStandard Recommendation) 2 and PSR4 are two specifications launched by the PHP community. They can not only improve the readability and maintainability of the code, but also provide consistent coding specifications in team collaboration. This article will introduce PSR2 and PSR4

PHP project version management and release process that complies with PSR2 and PSR4 specifications PHP project version management and release process that complies with PSR2 and PSR4 specifications Oct 15, 2023 am 10:27 AM

The PHP project version management and release process that complies with PSR2 and PSR4 specifications requires specific code examples. Introduction: In the process of developing PHP projects, it is a good habit to comply with coding standards. Among them, the PSR2 specification proposed by the PHP-FIG organization is the basic basis for the PHP coding specification, while the PSR4 specification is about automatic loading. This article will introduce how to comply with PSR2 and PSR4 specifications in PHP projects and give corresponding code examples. 1. PSR2 specification The PSR2 specification covers how

A preliminary study on PHP PSR2 and PSR4 specifications A preliminary study on PHP PSR2 and PSR4 specifications Oct 15, 2023 pm 03:33 PM

Preliminary study of PHPPSR2 and PSR4 specifications Introduction: In the process of writing PHP code, it is very important to follow certain coding specifications. Good coding standards can improve the readability and maintainability of code and facilitate teamwork. PHP has a series of coding specifications, of which PSR2 and PSR4 are the two most widely used specifications. This article will focus on the PSR2 and PSR4 specifications and illustrate how to follow these specifications through specific code examples. 1. PSR2 specification The PSR2 specification mainly focuses on PHP code.

See all articles