個人的習慣是駝峰式命名法,類別使用大駝峰,方法變量使用小駝峰,常量定義全大寫,全域 g_,私有 _ 底線開頭,內變量 m_,在轉換學習CI3時發現有一點點不一樣,在此筆記一下,順便列出一些慣用的觀念,所以以下的錯誤不見得是真的錯誤。這些資訊在 CodeIgniter 官方文件中都可以找到,需要更詳細資訊的可以前往官網 https://codeigniter.com 查詢。

CodeIgniter 3 


文件使用 Unicode (UTF-8, no BOM)

命名規則(不用駝峰式,僅字首可以大寫,連字號使用底線 _ )

  1. [類別文件名稱] 必須 [以大寫字母為字首] (僅字首可以大寫),其他文件如:model、view,腳本文件等以全小寫命名
    • myclass.php ->錯誤 字首須大寫
    • MyClass.php ->錯誤 僅字首可以大寫
    • MYCLASS.php ->錯誤 僅字首可以大寫
    • My_Class.php ->錯誤 僅字首可以大寫
    • Myclass.php ->正確
    • My_class.php ->正確
       
  2. [類別文件名稱] 必須和 [類別名稱] 保持一致,例如:類別名稱為 Myclass,那此類別文件名稱應該為 Myclass.php。
     
  3. 類別名稱字首必須為大寫英文字母,多個單詞之間用底線 _ 分隔。
    • class myclass ->錯誤 字首須大寫
    • class MyClass ->錯誤 僅字首可以大寫
    • class Myclass ->正確
    • class My_class ->正確
       
  4. [類別的方法名稱] 應該使用 [全小寫],最好包含一個動詞並且明確指出該方法的功能,多單詞之間使用底線 _  分隔。
    • 錯誤
    • function fileproperties() // not descriptive and needs underscore separator
    • function fileProperties() // not descriptive and uses CamelCase
    • function getfileproperties() // Better! But still missing underscore separator
    • function getFileProperties() // uses CamelCase
    • function get_the_file_properties_from_the_file() // wordy
    • 正確
    • function get_file_properties()  // descriptive, underscore separator, and all lowercase letter

以上4點是之前在使用CI2~CI3時遇到的命名問題

* 補充:
Model 文件名全小寫 例:member_model.php , 這裡有點錯,更新一下,這是在 CI2 的命名

在CI3 中 Model 的類別名稱字首需為英文大寫(僅字首),文件名稱與類別名稱一致。

model calss name 加後綴 _model(非必須)以便與 Controller 的類有所區隔

例:

控制器 class Member extends MY_Controller { ... }  // 文件名稱 Member.php

模型:class Member_model extends CI_Model { ... } // 文件名稱 Member_model.php

調用模型時為全小寫 $this->load->model('member_model');

 


 

  • 變量命名規則與方法命名接近,使用全小寫,多單詞以底線 _ 分隔,明確指出該變量的用途,無意義的短變數應只使用在 for loop 中使用。
    • 錯誤
      • $j = 'foo'; // single letter variables should only be used in for() loops
      • $Str // contains uppercase letters
      • $bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning
      • $groupid // multiple words, needs underscore separator
      • $name_of_last_city_used // too long
    • 正確
      • for ($j = 0; $j < 10; $j++)
      • $str
      • $buffer
      • $group_id
      • $last_city
         
  • 註解的使用:DocBlock 風格的註解,寫在類別、方法及屬性定義前,可以被IDE辦識

/**
* Super Class
*
* @package Package Name
* @subpackage Subpackage
* @category Category
* @author Author Name
* @link http://example.com
*/

class Super_class
{

/**
* Data for class manipulation
*
* @var array
*/

public $data = array();
............

/**
* Encodes string for use in XML
*
* @param string $str Input string
* @return string
*/

public function xml_encode($str)
{
        ...............
}

}

單行註解應該和代碼合在一起,大塊的註解和代碼間保留一空行。

// break up the string by newlines
$parts = explode("\n", $str);

// A longer comment that needs to give greater detail on what is
// occurring and why can use multiple single-line comments. Try to
// keep the width reasonable, around 70 characters is the easiest to
// read. Don't hesitate to link to permanent external resources
// that may provide greater detail:
//
// http://example.com/information_about_something/in_particular/

$parts = $this->foo($parts);

  • 常數:使用全大寫,多單詞以底線 _ 分隔,明確指出該變量的用途。儘量使用 CodeIgniter已經定義好的。
     
  • TRUE、FALSE、NULL 關鍵字使用大寫。
     
  • 邏輯符號:建議在 ! 前後加一個空格。
     
  • 返迴值的比較,有些函式在失敗時返廻 FALSE ,但也可能返廻 "", NULL , 或 0 ,所以請使用嚴格類型比較 === ,!=== 。
     
  • 文件中的空格:php起始標籤前與結束標籤後面都不要留空格,輸出是被緩存的,所以如果文件中有空格會在CodeIgniter輸出它的內容前被輸出,從而導致錯誤,也會導致CodeIgniter無法發送正確的標頭訊息。
     
  • 遵循一個類別一個文件,除非幾個類別是『緊密相關』的。在CodeIgniter中一個文件包含多個類的一個例子是 Xmlrpc 類別文件。
     
  • 使用 tab 代替空格,開發者可以根據喜好定義 tab字符。
     
  • 換行:使用 Unix 的換行格式保存。
     
  • 縮排:使用 Allman 風格,所有大括號都保持獨立一行。
     
  • 中括號和小括號內不應該有多餘的空格。
     
  • 私有方法和變量,只能在內部訪問的方法和變量,以底線 _ 開頭。
    • public fucntion convert_text()
    • private fucntion _convert_text()
       
  • 使用完整的PHP標記
    • 錯誤
      • <? echo $foo; ?>
        # 原本在 php.ini 裡頭 short_open_tag = Off,此為預設值,在此設定下撰寫 PHP 程式必須以 <?php 開頭
         
      • <?=$foo?>
        # php 5.4 以後版本 <?= 相等於 <? echo   就算不開啟𥬢短標籤也是可以直接使用短標籤,但還是不建議使用
    • 正確
      • <?php echo $foo; ?>
         
  • 每一行只寫一條語句,不要併在同一行。
    • 錯誤
      • $foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag);
    • 正確
      • $foo = 'this';
      • $bar = 'that';
      • $bat = str_replace($foo, $bar, $bag);
         
  • 字串,純字串使用單引號框,當字串中有變量時使用雙引號框,並且使用大括號將變量包起來,當字串中有單引號時使用雙引號框,這樣就不用使用轉義符號。
    • 錯誤
      • "My String"                 // no variable parsing, so no use for double quotes
      • "My string $foo"        // needs braces
      • 'SELECT foo FROM bar WHERE baz = \'bag\''   // ugly
    • 正確
      • 'My String'
      • "My string {$foo}"
      • "SELECT foo FROM bar WHERE baz = 'bag' "
         
  • SQL 語句,SQL關鍵字永遠使用大寫:SELECT, INSERT, UPDATE, FROM, WHERE, JOIN, ON, IN, AS, LIMIT,最好是每行只有一個從句或子從句,以增加易讀性。 
    • 錯誤
      • // keywords are lowercase and query is too long for
        // a single line (... indicates continuation of line)
        $query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses
        ...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100");
    • 正確
      • $query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz
                        FROM exp_pre_email_addresses
                        WHERE foo != 'oof'
                        AND baz != 'zab'
                        ORDER BY foobaz
                        LIMIT 5, 100");
         
  • 缺省的函數參數,在適當的時候提供參數的缺省值,有助於防止因錯誤的函數調用而引起的PHP錯誤,例:
    • function foo($bar = '', $baz = FALSE)

 

 

arrow
arrow
    文章標籤
    CodeIgniter coding 命名
    全站熱搜

    Godspeed 發表在 痞客邦 留言(0) 人氣()