跳至內容

PHP 程式設計/匈牙利命名法變體

來自華夏公益教科書,開放的書籍,開放的世界


如何開始使用 PHP 匈牙利命名法變體


匈牙利命名法 是一種 程式語言變數命名約定。自 1999 年左右,來自匈牙利的查爾斯·西蒙尼 引入這種命名約定 以來,一些人嘗試將其適應於各種新的程式語言。它不僅可以幫助人們理解變數的用途,還可以幫助人們理解變數中包含的預期資料型別。

對於 PHP,PHP 匈牙利命名法變體(或PAHN)是基於匈牙利命名法為 PHP 制定命名約定的嘗試,但採用更簡化的格式,並解決 PHP 語言與西蒙尼當時使用的語言之間的差異。

  • 透過使用與函式、類方法或類變數名稱不同的變數命名約定,它有助於使程式碼更易於閱讀,從而避免將變數與函式呼叫混淆,例如。
  • 它可以幫助其他程式設計師回到您的專案,理解該變數的意圖。
  • 透過堅持像 PAHN 這樣的簡單標準,它很容易學習,是(許多)使來自多個團隊成員的程式碼看起來相同的措施之一。否則,一個團隊成員可能使用一個變數命名約定,而另一個團隊成員可能使用另一個。
  • 沒有很多關於基於 PHP 的變數命名約定的已釋出標準。透過在這裡設定一個,這是解決這個問題的一個機會,也是在網路上提供一箇中央文件,以便許多人可以參考。
  • 用於模型物件的類變數,例如 $Member,需要比 $nMember 或 $sMember 更突出,例如。使用命名約定有助於提高可讀性,以便區分兩者。
  • 想象一下,我們想區分 $sMemberID 表示可能為字母數字的 ID,而 $nMemberID 將是一個整數。如果我們使用 $MemberID,你就不會那麼容易理解。所以,同樣,這種命名約定提高了可讀性。
  • 如果我們使用 $NamesArray,它比 $asNames 輸入更多。此外,我們不知道 $NamesArray 是一個 Names 物件陣列,一個 Names 字串陣列,還是什麼。所以,同樣,我們可以透過使用這種命名約定提高程式碼的可讀性。

PAHN 變數命名約定以一系列字首字元開頭,後跟 ProperCase 變數名。示例

 global $gasNames;
 $gasNames = $Members->getNames();

$gasNames 將表示全域性 + 陣列 + 字串,或全域性字串陣列。

字首是

_ = a private class variable
a+ = array (often combined with the data type used inside the array)
c+ = character
s+ = string
o+ = object
d+ = date object -- as in what's returned from a date() or gmdate()
v+ = variant -- used very infrequently to mean any kind of possible variable type
i+ = integer -- an integer
f+ = float -- a floating point number, e.g. an integer with a fractional part
n+ = numeric (unknown if it's float, integer, etc. Use infrequently)
x+ = to let other programmers know that this is a variable intended to be used by reference rather than value
rs+ = db recordset (set of rows)
rw+ = db row
h+ = handle, as in db handle, file handle, connection handle, curl handle, socket handle, etc.
hf = handle to function, as in setRetrievalStrategy(callable $hfStrategy)
t+ = a threaded object, use to indicate that an object may be safe to call\pass between threads
g+ = global var (and used sparingly, and often combined with the datatype used for the variable)
b+ = boolean

一些示例

 $oMember -- a Member object
 $hFile -- a handle to a file, for instance as passed from the fopen() statement
 $cFirst -- first character retrieved from a string
 $rsMembers -- records of Members, as returned from a database table
 $rwMember -- a single Member record from the database
 $bUseNow -- a boolean flag
 $sxMemberName -- a byref string variable for a name of a member.
 $nCounter -- a numeric counter
 $dBegin -- a beginning date
 $sFirstName -- a string to represent someone's first name
 $_hDB -- a private class variable to store a database connection handle (often addressed by $this->_hDB)

對於類變數名,PAHN 不使用此字首。因此,您可能會看到類似的東西

 $Members = new Members();

對於常量,PAHN 只使用一個大寫單詞,例如 MEMBER,這通常只用於在 PHP 替代語法CCAPS 中插入變數。

至於字首之後的內容,最好堅持使用 ProperCase,如 $sMemberName 而不是 $sMEMBERNAME、$sMember_Name、$s_Member_Name 或 $smemberName。這有幾個原因。在 $sMEMBERNAME 的情況下,它意味著該變數應該像常量一樣對待(常量通常使用大寫字母),而實際上並非如此。在 $sMember_Name 中,它會導致不必要的輸入,就像 $s_Member_Name 一樣。而 $smemberName 將 s+ 字首與單詞“member”結合在一起,導致變數名令人困惑。現在,也就是說,在某些情況下,新增下劃線確實有助於可讀性,在這些情況下,它可以與 PAHN 一起使用。此罕見例外的很好的例子是使用縮寫,例如 FIFO。因此,$bFIFOIndicator 可能比 $bFIFO_Indicator 更令人困惑,後者更受歡迎。

對於用作迴圈迭代器的變數,此字首也有一個例外。許多程式設計師可能熟悉許多教科書中使用的簡短變數:$a、$b、$c、$d、$i、$x、$y、$z。這些非常適合您想要使用一個在迴圈中定址並在陣列中使用的迭代器變數時。例如

$asMemberNames = array();
for ($i = 1; $i <= 10; $i++) {
  $asMemberNames[$i-1] = $Member->getMemberByID($i);
}

因此,允許此迴圈迭代器的例外,因為它可以節省輸入時間,並且不會降低可讀性。


華夏公益教科書