PHP代碼編寫(xiě)規(guī)范技巧總結(jié)-創(chuàng)新互聯(lián)

這篇文章為大家?guī)?lái)有關(guān)PHP代碼編寫(xiě)規(guī)范詳細(xì)介紹。大部分PHP知識(shí)點(diǎn)都是大家經(jīng)常用到的,為此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

績(jī)溪網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,績(jī)溪網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為績(jī)溪1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的績(jī)溪做網(wǎng)站的公司定做!

差:

<?php class Car{
    public $carMake;
    public $carModel;
    public $carColor;
    //...
    }

好:

<?php class Car{
    public $make;
    public $model;
    public $color;
    //...
    }

函數(shù)參數(shù)數(shù)量(理想情況是 2 個(gè)以下)

限制函數(shù)參數(shù)的數(shù)量是非常重要的,因?yàn)樗尯瘮?shù)更容易測(cè)試,參數(shù)超過(guò)三個(gè)的話(huà),你必須用每個(gè)單獨(dú)的參數(shù)測(cè)試大量不同的情況。

無(wú)參數(shù)是理想的情況。一個(gè)或兩個(gè)參數(shù)是可以的,但應(yīng)該避免三個(gè)。通常,如果你有兩個(gè)以上的參數(shù),那么你的函數(shù)試圖完成太多的功能,若不是,大多數(shù)時(shí)候,較高級(jí)的對(duì)象就足以作為參數(shù)(譯者注:比如數(shù)組、對(duì)象)。

差:

<?php function createMenu($title, $body, $buttonText, $cancellable) {
    // ...}

好:

<?php class MenuConfig {
    public $title;
    public $body;
    public $buttonText;
    public $cancellable = false;}$config = new MenuConfig();$config->title = 'Foo';$config->body = 'Bar';$config->buttonText = 'Baz';$config->cancellable = true;function createMenu(MenuConfig $config) {
    // ...}

一個(gè)函數(shù)應(yīng)該只完成一件事

這是軟件工程中最重要的規(guī)則。當(dāng)函數(shù)做的事多于一件事情時(shí),他們更難編寫(xiě)和測(cè)試。 當(dāng)你可以將函數(shù)隔離成一個(gè)動(dòng)作時(shí),可以輕松重構(gòu),代碼也將更易讀。

差:

<?phpfunction emailClients($clients) {
    foreach ($clients as $client) {
        $clientRecord = $db->find($client);
        if ($clientRecord->isActive()) {
            email($client);
        }
    }}

好:

function emailClients($clients) {
    $activeClients = activeClients($clients);
    array_walk($activeClients, 'email');
}
function activeClients($clients) {
    return array_filter($clients, 'isClientActive');
}
function isClientActive($client) {
    $clientRecord = $db->find($client);
    return $clientRecord->isActive();
}

使用 get 和 set 方法

在 PHP 中,可以為方法設(shè)置 public、protected 和 private 關(guān)鍵字,可以控制對(duì)象上的屬性可見(jiàn)性。這是面向?qū)ο蟮脑O(shè)計(jì)原則中的開(kāi)放/封閉原則的一部分。

差:

class BankAccount
{
    public $balance = 1000;
}
$bankAccount = new BankAccount();
// Buy shoes...
$bankAccount->balance -= 100;

好:

class BankAccount
{
    private $balance;
    public function __construct($balance = 1000)
    {
      $this->balance = $balance;
    }
    public function withdrawBalance($amount)
    {
        if ($amount > $this->balance) {
            throw new \Exception('Amount greater than available balance.');
        }
        $this->balance -= $amount;
    }
    public function depositBalance($amount)
    {
        $this->balance += $amount;
    }
    public function getBalance()
    {
        return $this->balance;
    }
}
$bankAccount = new BankAccount();
// Buy shoes...
$bankAccount->withdrawBalance($shoesPrice);
// Get balance
$balance = $bankAccount->getBalance();

關(guān)于PHP代碼編寫(xiě)規(guī)范就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。

網(wǎng)站題目:PHP代碼編寫(xiě)規(guī)范技巧總結(jié)-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://bm7419.com/article12/dseddc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、虛擬主機(jī)定制開(kāi)發(fā)、手機(jī)網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)