溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP編程的五個(gè)良好習(xí)慣是什么

發(fā)布時(shí)間:2021-10-18 16:20:51 來源:億速云 閱讀:150 作者:柒染 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)PHP編程的五個(gè)良好習(xí)慣是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

根據(jù)具體的情況,一般的開發(fā)人員往往比優(yōu)秀的開發(fā)人員的效率低 10%~20%。優(yōu)秀的開發(fā)人員的效率更高,因?yàn)樗麄儞碛胸S富的經(jīng)驗(yàn)和良好的編程習(xí)慣。不良的編程習(xí)慣將會(huì)影響到效率。本文通過展示一些良好的編程習(xí)慣,幫助您成為更優(yōu)秀的程序員。

這些良好的編程習(xí)慣不僅能提高效率,還能讓您編寫出在應(yīng)用程序的整個(gè)生命周期中易于維護(hù)的代碼。編寫出來的代碼可能需要大量的維護(hù);應(yīng)用程序的維護(hù)是一筆很 大的開支。養(yǎng)成良好的編程習(xí)慣能夠提高設(shè)計(jì)質(zhì)量(比如模塊化),從而使代碼更加容易理解,因此維護(hù)就更加容易,同時(shí)也降低維護(hù)成本。

不良的編程習(xí)慣會(huì)造成代碼缺陷,使其難以維護(hù)和修改,并且很可能在修改時(shí)又引入其他缺陷。以下是 5 個(gè)良好的編程習(xí)慣,能夠幫助 PHP 代碼避免這些缺陷:

◆使用良好的命名。

◆分成更小的部分。

◆為代碼添加注釋。

◆處理錯(cuò)誤條件。

◆切忌使用復(fù)制粘貼。

下面將詳細(xì)介紹這些習(xí)慣:

使用良好的命名

使用良好的命名是最重要的編程習(xí)慣,因?yàn)槊枋鲂詮?qiáng)的名稱讓代碼更加容易閱讀和理解。代碼是否好理解取決于是否能在未來維護(hù)它。即便代碼不帶有注釋,如果它很容易理解,將大大方便日后的更改。這個(gè)習(xí)慣的目標(biāo)是讓您編寫的代碼像書本一樣容易閱讀和理解。

不良習(xí)慣:含糊的或無意義的名稱

清單 1 中的代碼包含過短的變量名、難以辨認(rèn)的縮寫詞,并且方法名不能反映該方法的功能。如果方法名給人的感覺是它應(yīng)該做這件事情,而實(shí)際中它卻做另外的事情,這將帶來嚴(yán)重的問題,因?yàn)樗鼤?huì)誤導(dǎo)人。

清單 1. 不良習(xí)慣:含糊的或無意義的名稱

<?php

function getNBDay($d)
{
    switch($d) {
        case 5:
        case 6:
        case 7:
            return 1;
        default:
            return ($d + 1);
    }
}

$day = 5;

$nextDay = getNBDay($day);

echo ("Next day is: " . $nextDay . "n");

?>

良好習(xí)慣:說明性強(qiáng)并且簡潔的名稱

清單 2 中的代碼體現(xiàn)了良好的編程習(xí)慣。新的方法名具有很強(qiáng)的說明性,反映了方法的用途。同樣,更改后的變量名也更具說明性。惟一的保持最短的變量是 $i,在本清單中,它是一個(gè)循環(huán)變量。盡管很多人不贊同使用過短的名稱,但在循環(huán)變量中使用還是可以接受的(甚至有好處),因?yàn)樗鞔_表明了代碼的功能。

清單 2. 良好習(xí)慣:說明性強(qiáng)并且簡潔的名稱

<?php
define ('MONDAY', 1);
define ('TUESDAY', 2);
define ('WEDNESDAY', 3);
define ('THURSDAY', 4);
define ('FRIDAY', 5);
define ('SATURDAY', 6);
define ('SUNDAY', 7);
/*
 *
 * @param $dayOfWeek
 * @return int Day of week, with 1 being Monday and so on.
 */
function findNextBusinessDay($dayOfWeek)
{
    $nextBusinessDay = $dayOfWeek;
    switch($dayOfWeek) {
        case FRIDAY:
        case SATURDAY:
        case SUNDAY:
            $nextBusinessDay = MONDAY;
            break;
        default:
            $nextBusinessDay += 1;
            break;
    }
     return $nextBusinessDay;
}
$day = FRIDAY;
$nextBusDay = findNextBusinessDay($day);
echo ("Next day is:" . $nextBusDay . "n");
?>

我們鼓勵(lì)您將大的條件拆分為一個(gè)方法,然后用能夠描述該條件的名字命名方法。這個(gè)技巧能夠提高代碼的可讀性,并且能夠?qū)l件具體化,使之能夠被提取甚至重用。如果條件發(fā)生變化,更新方法也很容易。因?yàn)榉椒〒碛幸粋€(gè)有意義的名字,所以它能反映代碼的用途,讓代碼更容易閱讀。

分成更小的部分

專心解決一個(gè)問題之后再繼續(xù)編程,這樣會(huì)讓您更輕松。在解決一個(gè)緊急的問題時(shí),如果繼續(xù)編程,會(huì)使函數(shù)越來越長。從長遠(yuǎn)來說,這并不是一個(gè)問題,但您要記得回過頭來將它重構(gòu)為更小的部分。

重構(gòu)是個(gè)不錯(cuò)的主意,但您應(yīng)該養(yǎng)成編寫更短、功能更集中的代碼。短的方法能夠在一個(gè)窗口中一次看完,并且容易理解。如果方法過長,不能在一個(gè)窗口中一次看完,那么它就變得不容易理解,因?yàn)槟荒芸焖俚貜念^到尾了解它的整個(gè)思路。

構(gòu)建方法時(shí),您應(yīng)該養(yǎng)成這樣的習(xí)慣,讓每個(gè)方法只完成一件事情。這個(gè)習(xí)慣很好,因?yàn)椋菏紫?,如果方法只完成一件事情,那么它就更容易被重用;其次,這樣的方法容易測試;第三,這樣的方法便于理解和更改。

不良習(xí)慣:過長的方法(完成很多件事情)

清單 3 展示了一個(gè)很長的函數(shù),其中存在很多問題。它完成很多件事情,因此不夠緊湊。它也不便于閱讀、調(diào)試和測試。它要做的事情包括遍歷一個(gè)文件、構(gòu)建一個(gè)列表、為每個(gè)對象賦值、執(zhí)行計(jì)算等等。

清單 3. 不良習(xí)慣:過長的函數(shù)

<?php

function writeRssFeed($user)
{
    // Get the DB connection information
    
    
    // look up the user's preferences...
    $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
        OR die(mysql_error());

    // Query
    $perfsQuery = sprintf("SELECT max_stories FROM user_perfs WHERE user= '%s'",
            mysql_real_escape_string($user));

    $result = mysql_query($query, $link);
    
    $max_stories = 25; // default it to 25;
    
    if ($row = mysql_fetch_assoc($result)) {
        $max_stories = $row['max_stories'];
    }
            
    // go get my data
    $perfsQuery = sprintf("SELECT * FROM stories WHERE post_date = '%s'",
            mysql_real_escape_string());
            
    $result = mysql_query($query, $link); 


    $feed = "<rss version="2.0">" .
        "<channel>" .
        "<title>My Great Feed</title>" .
        "<link>http://www.example.com/feed.xml</link>" .
        "<description>The best feed in the world</description>" .
        "<language>en-us</language>" .
        "<pubDate>Tue, 20 Oct 2008 10:00:00 GMT</pubDate>" .
        "<lastBuildDate>Tue, 20 Oct 2008 10:00:00 GMT</lastBuildDate>" .
        "<docs>http://www.example.com/rss</docs>" .
        "<generator>MyFeed Generator</generator>" .
        "<managingEditor>editor@example.com</managingEditor>" .
        "<webMaster>webmaster@example.com</webMaster>" .
        "<ttl>5</ttl>";
    
        // build the feed...
        while ($row = mysql_fetch_assoc($result)) {
            $title = $row['title'];
            $link = $row['link'];
            $description = $row['description'];
            $date = $row['date'];
            $guid = $row['guid'];

            $feed .= "<item>";
            $feed .= "<title>" . $title . "</title>";
            $feed .= "<link>" . $link . "</link>";
            $feed .= "<description> " . $description . "</description>";
            $feed .= "<pubDate>" . $date . "</pubDate>";
            $feed .= "<guid>" . $guid . "</guid>";
            $feed .= "</item>";
        }

        $feed .= "</rss";

        // write the feed out to the server...
        echo($feed);

}

?>

如果多編寫幾個(gè)這樣的方法,維護(hù)就成了真正的難題了。

良好習(xí)慣:易管理、功能專一的方法

清單 4 將原來的方法改寫為更加緊湊、易讀的方法。在這個(gè)示例中,將一個(gè)很長的方法分解為幾個(gè)短方法,并且讓每個(gè)短方法負(fù)責(zé)一件事情。這樣的代碼對將來的重用和測試都是大有裨益的。

清單 4. 良好習(xí)慣:易管理、功能專一的方法

<?php

function createRssHeader()
{
    return "<rss version="2.0">" .
        "<channel>" .
        "<title>My Great Feed</title>" .
        "<link>http://www.example.com/feed.xml</link>" .
        "<description>The best feed in the world</description>" .
        "<language>en-us</language>" .
        "<pubDate>Tue, 20 Oct 2008 10:00:00 GMT</pubDate>" .
        "<lastBuildDate>Tue, 20 Oct 2008 10:00:00 GMT</lastBuildDate>" .
        "<docs>http://www.example.com/rss</docs>" .
        "<generator>MyFeed Generator</generator>" .
        "<managingEditor>editor@example.com</managingEditor>" .
        "<webMaster>webmaster@example.com</webMaster>" .
        "<ttl>5</ttl>";
}

function createRssFooter()
{
    return "</channel></rss>";
}

function createRssItem($title, $link, $desc, $date, $guid) 
{
    $item .= "<item>";
    $item .= "<title>" . $title . "</title>";
    $item .= "<link>" . $link . "</link>";
    $item .= "<description> " . $description . "</description>";
    $item .= "<pubDate>" . $date . "</pubDate>";
    $item .= "<guid>" . $guid . "</guid>";
    $item .= "</item>";
    return $item;
}

function getUserMaxStories($db_link, $default)
{
    $perfsQuery = sprintf("SELECT max_stories FROM user_perfs WHERE user= '%s'",
            mysql_real_escape_string($user));

    $result = mysql_query($perfsQuery, $db_link);
    
    $max_stories = $default;
    
    if ($row = mysql_fetch_assoc($result)) {
        $max_stories = $row['max_stories'];
    } 
    
    return $max_stories;
}

function writeRssFeed($user)
{
    // Get the DB connection information
    $settings = parse_ini_file("rss_server.ini");
    
    // look up the user's preferences...
    $link = mysql_connect($settings['db_host'], $settings['user'], 
        $settings['password']) OR die(mysql_error());

    $max_stories = getUserMaxStories($link, 25);
        
    // go get my data
    $newsQuery = sprintf("SELECT * FROM stories WHERE post_date = '%s'",
            mysql_real_escape_string(time()));
            
    $result = mysql_query($newsQuery, $link); 

    $feed = createRssHeader();
    
    $i = 0;
    // build the feed...
    while ($row = mysql_fetch_assoc($result)) {
        if ($i < $max_stories) {
            $title = $row['title'];
            $link = $row['link'];
            $description = $row['description'];
            $date = $row['date'];
            $guid = $row['guid'];

            $feed .= createRssItem($title, $link, $description, $date, $guid);
            
            $i++;
        } else { 
            break;
        }
    }
    
    mysql_close($link);

    $feed .= createRssFooter();

    // write the feed out to the server...
    echo($feed);
}
?>

將長方法拆分為短方法也是有限制的,過度拆分將適得其反。因此,不要濫用這個(gè)良好的習(xí)慣。將代碼分成大量的片段就像沒有拆分長代碼一樣,都會(huì)造成閱讀困難。

為代碼添加注釋

要為代碼添加良好的注釋有時(shí)似乎和編寫代碼一樣難。要了解應(yīng)該為哪些內(nèi)容添加注釋并不容易,因?yàn)槲覀兂3A向于注釋代碼當(dāng)前做的事情。注釋代碼的目的是不錯(cuò)的主意。在函數(shù)的不是很明顯的頭部代碼塊中,告訴讀者方法的輸入和輸出,以及方法的最初目標(biāo)。

注釋代碼當(dāng)前做什么是很常見的,但這是不必要的。如果代碼很復(fù)雜,不得不注釋它當(dāng)前在做什么,這將暗示您應(yīng)該重寫代碼,讓它更容易理解。學(xué)會(huì)使用良好的名稱和更短的方法,在不提供注釋說明其用途的情況下提高代碼的可讀性。

不良習(xí)慣:函數(shù)注釋過多或不足

清單 5 中的注釋僅告訴讀者代碼在做什么 — 它正在通過一個(gè)循環(huán)進(jìn)行迭代或添加一個(gè)數(shù)字。但它忽略了它為什么 做當(dāng)前的工作。這使維護(hù)該代碼的人員不知道是否可以安全地更改代碼(不引入新缺陷)。

清單 5. 不良習(xí)慣:函數(shù)注釋過多或不足

<?php

class ResultMessage 
{
    private $severity;
    private $message;
    
    public function __construct($sev, $msg) 
    {
        $this->severity = $sev;
        $this->message = $msg;
    }
    
    public function getSeverity()
    {
        return $this->severity;
    }
    
    public function setSeverity($severity)
    {
        $this->severity = $severity;
    }
    
    public function getMessage()
    {
        return $this->message;
    }
    
    public function setMessage($msg)
    {
        $this->message = $msg;
    }
}

function cntMsgs($messages)
{
    $n = 0;
    /* iterate through the messages... */
    foreach($messages as $m) {
        if ($m->getSeverity() == 'Error') {
            $n++; // add one to the result;
        }
    }
    return $n;
}

$messages = array(new ResultMessage("Error", "This is an error!"),
    new ResultMessage("Warning", "This is a warning!"),
    new ResultMessage("Error", "This is another error!"));
    
$errs = cntMsgs($messages);

echo("There are " . $errs . " errors in the result.n");

?>

良好習(xí)慣:帶注釋的函數(shù)和類

清單 6 中的注釋告訴讀者類和方法的目的。該注釋解釋了為什么代碼在做當(dāng)前的工作,這對未來維護(hù)代碼十分有用。可能需要根據(jù)條件變更而修改代碼,如果能夠輕松了解代碼的目的,則修改起來很容易。

清單 6. 良好習(xí)慣:帶注釋的函數(shù)和類

<?php
/**
 * The ResultMessage class holds a message that can be returned
 * as a result of a process. The message has a severity and
 * message.
 * 
 * @author nagood
 *
 */
class ResultMessage 
{
    private $severity;
    private $message;
    
    /**
     * Constructor for the ResultMessage that allows you to assign
     * severity and message.
     * @param $sev See {@link getSeverity()}
     * @param $msg
     * @return unknown_type
     */
    public function __construct($sev, $msg) 
    {
        $this->severity = $sev;
        $this->message = $msg;
    }
    
    /**
     * Returns the severity of the message. Should be one
     * "Information", "Warning", or "Error".
     * @return string Message severity
     */
    public function getSeverity()
    {
        return $this->severity;
    }
    
    /**
     * Sets the severity of the message
     * @param $severity
     * @return void
     */
    public function setSeverity($severity)
    {
        $this->severity = $severity;
    }
    
    public function getMessage()
    {
        return $this->message;
    }
    
    public function setMessage($msg)
    {
        $this->message = $msg;
    }
}


/*
 * Counts the messages with the given severity in the array
 * of messages.
 * 
 * @param $messages An array of ResultMessage
 * @return int Count of messages with a severity of "Error"
 */
function countErrors($messages)
{
    $matchingCount = 0;
    foreach($messages as $m) {
        if ($m->getSeverity() == "Error") {
            $matchingCount++;
        }
    }
    return $matchingCount;
}

$messages = array(new ResultMessage("Error", "This is an error!"),
    new ResultMessage("Warning", "This is a warning!"),
    new ResultMessage("Error", "This is another error!"));
    
$errs = countErrors($messages);

echo("There are " . $errs . " errors in the result.n");

?>

處理錯(cuò)誤

根據(jù)大眾的經(jīng)驗(yàn),如果要編寫健壯的應(yīng)用程序,錯(cuò)誤處理要遵循 80/20 規(guī)則:80% 的代碼用于處理異常和驗(yàn)證,20% 的代碼用于完成實(shí)際工作。在編寫程序的基本邏輯(happy-path)代碼 時(shí)經(jīng)常這樣做。這意味著編寫適用于基本條件的代碼,即所有的數(shù)據(jù)都是可用的,所有的條件符合預(yù)期。這樣的代碼在應(yīng)用程序的生命周期中可能很脆弱。另一個(gè)極端是,甚至需要花大量時(shí)間為從未遇到過的條件編寫代碼。

這一習(xí)慣要求您編寫足夠的錯(cuò)誤處理代碼,而不是編寫對付所有錯(cuò)誤的代碼,以致代碼遲遲不能完成。

不良習(xí)慣:根本沒有錯(cuò)誤處理代碼

清單 7 中的代碼演示了兩個(gè)不良習(xí)慣。***,沒有檢查輸入的參數(shù),即使知道處于某些狀態(tài)的參數(shù)會(huì)造成方法出現(xiàn)異常。第二,代碼調(diào)用一個(gè)可能拋出異常的方法,但沒有處理該異常。當(dāng)發(fā)生問題時(shí),代碼的作者或維護(hù)該代碼的人員只能猜測問題的根源。

清單 7. 不良習(xí)慣:不處理錯(cuò)誤條件

<?php

// Get the actual name of the 
function convertDayOfWeekToName($day)
{
    $dayNames = array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday");
    return $dayNames[$day];
}

echo("The name of the 0 day is:  " . convertDayOfWeekToName(0) . "n");
echo("The name of the 10 day is:  " . convertDayOfWeekToName(10) . "n");
echo("The name of the 'orange' day is:  " . convertDayOfWeekToName('orange') . "n");

?>

良好習(xí)慣:處理異常

清單 8 展示了以有意義的方式拋出和處理異常。額外的錯(cuò)誤處理不僅使代碼更加健壯,它還提高代碼的可讀性,使代碼更容易理解。處理異常的方式很好地說明了原作者在編寫方法時(shí)的意圖。

清單 8. 良好習(xí)慣:處理異常

<?php

/**
 * This is the exception thrown if the day of the week is invalid.
 * @author nagood
 *
 */
class InvalidDayOfWeekException extends Exception { }

class InvalidDayFormatException extends Exception { }

/**
 * Gets the name of the day given the day in the week. Will
 * return an error if the value supplied is out of range.
 * 
 * @param $day
 * @return unknown_type
 */
function convertDayOfWeekToName($day)
{
    if (! is_numeric($day)) {
        throw new InvalidDayFormatException('The value '' . $day . '' is an ' .
            'invalid format for a day of week.');
    }
    
    if (($day > 6) || ($day < 0)) {
        throw new InvalidDayOfWeekException('The day number '' . $day . '' is an ' .
            'invalid day of the week. Expecting 0-6.');
    }
    
    $dayNames = array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday");
    return $dayNames[$day];
}

echo("The name of the 0 day is:  " . convertDayOfWeekToName(0) . "n");

try {
    echo("The name of the 10 day is:  " . convertDayOfWeekToName(10) . "n");
} catch (InvalidDayOfWeekException $e) {
    echo ("Encountered error while trying to convert value:  " . $e->getMessage() . "n");
}

try {
    echo("The name of the 'orange' day is:  " . convertDayOfWeekToName('orange') . "n");
} catch (InvalidDayFormatException $e) {
    echo ("Encountered error while trying to convert value:  " . $e->getMessage() . "n");
}

?>

雖然檢查參數(shù)是一種確認(rèn) — 如果您要求參數(shù)處于某種狀態(tài),這將對使用方法的人很有幫助 — 但是您應(yīng)該檢查它們并拋出有意義的異常:

◆處理異常要盡量與出現(xiàn)的問題緊密相關(guān)。

◆專門處理每個(gè)異常。

切忌使用復(fù)制粘貼

您可以從其他地方將代碼復(fù)制粘貼到自己的代碼編輯器,但這樣做有利也有弊。好的一面是,從一個(gè)示例或模板中復(fù)制代碼能夠避免很多錯(cuò)誤。不好的一面是,這容易帶來大量的類似編程方式。

一定要注意,不要將代碼從應(yīng)用程序的一部分復(fù)制粘貼到另一部分。如果您采用這種方式,請停止這個(gè)不良的習(xí)慣,然后考慮將這段代碼重寫為可重用的。一般而言,將代碼放置到一個(gè)地方便于日后的維護(hù),因?yàn)檫@樣只需在一個(gè)地方更改代碼。

不良習(xí)慣:類似的代碼段

清單 9 給出了幾個(gè)幾乎一樣的方法,只是其中的值不同而已。有一些工具可以幫助找到復(fù)制粘貼過來的代碼(參見 參考資料)。

清單 9. 不良習(xí)慣:類似的代碼段

<?php
/**
 * Counts the number of messages found in the array of 
 * ResultMessage with the getSeverity() value of "Error"
 * 
 * @param $messages An array of ResultMessage
 * @return unknown_type
 */
function countErrors($messages)
{
    $matchingCount = 0;
    foreach($messages as $m) {
        if ($m->getSeverity() == "Error") {
            $matchingCount++;
        }
    }
    return $matchingCount;
}

/**
 * Counts the number of messages found in the array of 
 * ResultMessage with the getSeverity() value of "Warning"
 * 
 * @param $messages An array of ResultMessage
 * @return unknown_type
 */
function countWarnings($messages)
{
    $matchingCount = 0;
    foreach($messages as $m) {
        if ($m->getSeverity() == "Warning") {
            $matchingCount++;
        }
    }
    return $matchingCount;
}

/**
 * Counts the number of messages found in the array of 
 * ResultMessage with the getSeverity() value of "Information"
 * 
 * @param $messages An array of ResultMessage
 * @return unknown_type
 */
function countInformation($messages)
{
    $matchingCount = 0;
    foreach($messages as $m) {
        if ($m->getSeverity() == "Information") {
            $matchingCount++;
        }
    }
    return $matchingCount;
}

$messages = array(new ResultMessage("Error", "This is an error!"),
    new ResultMessage("Warning", "This is a warning!"),
    new ResultMessage("Error", "This is another error!"));
    
$errs = countErrors($messages);

echo("There are " . $errs . " errors in the result.n");
?>

良好習(xí)慣:帶參數(shù)的可重用函數(shù)

清單 10 展示了修改后的代碼,它將復(fù)制的代碼放到一個(gè)方法中。另一個(gè)方法也進(jìn)行了更改,它現(xiàn)在將任務(wù)委托給新的方法。構(gòu)建通用的方法需要花時(shí)間設(shè)計(jì),并且這樣做使您能停下來思考,而不是本能地使用復(fù)制粘貼。但有必要進(jìn)行更改時(shí),對通用的方法投入的時(shí)間將得到回報(bào)。

清單 10. 良好習(xí)慣:帶參數(shù)的可重用函數(shù)

<?php
    /*
     * Counts the messages with the given severity in the array
     * of messages.
     * 
     * @param $messages An array of ResultMessage
     * @return int Count of messages matching $withSeverity
     */
    function countMessages($messages, $withSeverity)
    {
        $matchingCount = 0;
        foreach($messages as $m) {
            if ($m->getSeverity() == $withSeverity) {
                $matchingCount++;
            }
        }
        return $matchingCount;
    }

    /**
     * Counts the number of messages found in the array of 
     * ResultMessage with the getSeverity() value of "Error"
     * 
     * @param $messages An array of ResultMessage
     * @return unknown_type
     */
    function countErrors($messages)
    {
        return countMessages($messages, "Errors");
    }

    /**
     * Counts the number of messages found in the array of 
     * ResultMessage with the getSeverity() value of "Warning"
     * 
     * @param $messages An array of ResultMessage
     * @return unknown_type
     */
    function countWarnings($messages)
    {
        return countMessages($messages, "Warning");
    }

    /**
     * Counts the number of messages found in the array of 
     * ResultMessage with the getSeverity() value of "Warning"
     * 
     * @param $messages An array of ResultMessage
     * @return unknown_type
     */
    function countInformation($messages)
    {
        return countMessages($messages, "Information");
    }

    $messages = array(new ResultMessage("Error", "This is an error!"),
        new ResultMessage("Warning", "This is a warning!"),
        new ResultMessage("Error", "This is another error!"));
        
    $errs = countErrors($messages);

    echo("There are " . $errs . " errors in the result.n");

?>

結(jié)束語

如果您在編寫 PHP 代碼的過程中養(yǎng)成本文討論的良好習(xí)慣,您將能夠構(gòu)建易讀、易理解、易維護(hù)的代碼。使用這種方式構(gòu)建的易維護(hù)代碼將降低調(diào)試、修復(fù)和擴(kuò)展代碼所面臨的風(fēng)險(xiǎn)。
使用良好的名稱和更短的方法能夠提高代碼的可讀性。注釋代碼的目的有利于代碼理解和擴(kuò)展。適當(dāng)?shù)靥幚礤e(cuò)誤會(huì)使代碼更加健壯。***,停止使用復(fù)制粘貼,保持代碼干凈,提高可重用性。

上述就是小編為大家分享的PHP編程的五個(gè)良好習(xí)慣是什么了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI