PHP Coding Standards

參考: Mantis : http://www.mantisbt.org/guidelines.php

中文翻譯: http://www.ichiayi.com/wikipage/tech/mantis_coding

First, start off by reading the PHP Coding Standards document. I’ve deviated in a few places but just about everything in the document applies here as well.

Above all, write code that is easy to read and maintain. Comment blocks of code and functions at all times. And get on my case if I deviate too much as well!

Naming Variables
  • Use all lower case.
  • Use _ to separate words, e.g. $green_color_value
  • Use descriptive names (except loop variables).
  • Loop variables can be of the usual variety: $i, $j, $k, etc.
  • Count variables should follow the format $*_count, e.g. $bug_count
  • Global variables should be prefixed with g_
  • Temporary variables should be prefixed with t_
  • Parameters and variables passed from forms that have been cleaned of any special SQL chars (i.e. slashes) should be prefixed with c_
  • Uncleaned function parameters should be prefixed with p_
  • Uncleaned variables passed from forms should be prefixed with f_
  • Other variables are prefixed with v_, v2_, etc.
  • Never prefix with l_ or o_ or q_ (visually confusing)
  • $query and $result should be used for SQL query and results respectively
Naming Functions
  • Use all lower case.
  • Use _ to separate words, e.g. setup_page_breaks()
  • Keep functions to 5 words or less
  • Functions that print should be prefixed with print_.
  • Try to use prefixes to group functions (i.e., email_, news_, etc.)
Naming Classes
  • Use FirstLetterOfWordIsCaptilized style
  • Variables that are class objects should have the prefix coo_
Naming Files
  • Use all lower case.
  • Use _ to separate words, e.g. view_new_bugs_page.php
  • Use .php file extensions
  • Filenames must be less than 32 characters in length. This plays nice with older file systems like Mac OS.
  • Included files should be suffixed by _inc.php
SQL formatting
  • UPPERCASE all SQL keywords:
    $query = "SELECT *
    	FROM $g_mantis_bug_table
    	WHERE id='1'";
  • Always assign a query string to a variable. This makes code easier to debug when problems occur. Do not create the query in the call to the function.
  • Break up SQL queries over multiple lines to be easy to read.
General Formatting
  • Use TABS with a size of 4
  • Follow the table formatting of existing pages
  • Use <?php ?> for php delimiters.
  • Try not to print/echo HTML unless it’s short or in a function loop
  • Do not use the EOF construct
Miscellaneous
  • Don’t use the ?: construct except in very rare cases. It is confusing and has a lot of bug potential.
  • Avoid magic numbers. The only magic numbers in use should be 1 and 0 and their meaning should be obvious.
Page Guidelines
  • The first item should be the copyright notice
  • At the bottom will be the footer information and closing print_ functions.
Braces and Parantheses
  • Parantheses should be right after a function name, e.g. function() not function ()
  • Parantheses should have a space right after a keyword (if, while, for), e.g. for (…)
  • Formatting of braces is illustrating below. We use unmatched placing.
  • Arrays should be referenced with no spaces, e.g. $arr[‘index’] not $arr[ ‘index’ ]
    for (...) {
        blah
    }
    
    or
    
    if (...) {
        blah
    }
  • if … else blocks should be in this format:
    if (...) {
        blah1
    } else {
        blah2
    }
Comparisons
  • The NOT operator should be placed next to its operand. No spaces, e.g. !$value
  • Parentheses should be used for grouping, especially with multiple comparisons, e.g. if ( ( null == $val ) && ( null == $val2 ) )
Strings
  • Use spaces around the string concatenation operator, e.g. ‘str ‘ . $value . ‘ str2’;
  • Use ‘ instead of " if there are no variables or special characters.
Comments
  • Use the # symbol for line commenting, not //
  • Use /* */ for block commenting unless you nest /* */ comments. Generally, only use this during development.
  • Use @@@ followed by a brief message (BROKEN, TEMPORARY, etc) as a "look at this" indicator. Leaving your name next to is a good idea. This way critical items can easily be found.
Editor Features
  • Search and replace in multiple files
  • Goto line number
  • Syntax highlighting
  • Adjustable TAB spacing

中文 字詞 文章 索引 對照規畫

tbl_term

term_id term
1 ipod
2 apple
3 nano
4 iphone

tbl_doc

doc_id text_content uptime
1 【蘋果先生】Moshi iGlaze Apple iPhone 3G 專用保護殼 黑/紅/白
2 iTunes Gift Card蘋果線上音樂商店預付卡儲值Apple iTunes Shop Ipod Nano Shuffle iphone Mp3電影歌曲下載促銷
3 Apple iPhone 3G (8G)  

tbl_lookup

id term_id doc_id
1 2 1
2 4 1
3 2 2
4 1 2
5 3 2
6 4 2
7 2 3
8 4 3

解決 error while loading shared libraries 的方法

執行程式時, 若遇到

./update_item: error while loading shared libraries: libsqlrclientwrapper-0.40.so.1: cannot open shared object file: No such file or directory

像這樣的錯誤訊息 , 表示程式中所需的 so 檔(share library) 沒找到 , 要修改 /etc/ld.so.conf 加上這個目錄 , 改完要下 ldconfig –v 讓路徑生效

可以用 ldd 看該程式所使用到的 shared library 有那些, 及 lib 路徑

—–

以下是 gentoo 的標準解法 , 其他版本的 linux 則不太相同!!

cat /etc/ld.so.conf   這個檔是由 env-update 產生的 , 所以再繼續看一下 /etc/env.d 下 , 究竟有啥

# ld.so.conf autogenerated by env-update; make all changes to
# contents of /etc/env.d directory
/usr/local/lib
/usr/i686-pc-linux-gnu/lib
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2
/usr/lib/gcc/i686-pc-linux-gnu/4.1.1
/usr/local/firstworks/lib

參考資料, 這是 compile sqlrelay 列出來的 information

See any operating system documentation about shared libraries for

more information, such as the ld(1) and ld.so(8) manual pages.

———————————————————————-

/bin/sh ../../libtool –mode=finish /usr/local/firstworks/lib

PATH=”$PATH:/sbin” ldconfig -n /usr/local/firstworks/lib

———————————————————————-

Libraries have been installed in:

/usr/local/firstworks/lib

If you ever happen to want to link against installed libraries

in a given directory, LIBDIR, you must either use libtool, and

specify the full pathname of the library, or use the `-LLIBDIR’

flag during linking and do at least one of the following:

– add LIBDIR to the `LD_LIBRARY_PATH’ environment variable

during execution

– add LIBDIR to the `LD_RUN_PATH’ environment variable

during linking

– use the `-Wl,–rpath -Wl,LIBDIR’ linker flag

– have your system administrator add LIBDIR to `/etc/ld.so.conf’

在 /etc/env.d 下建立一個 90sqlrelay

內容是

LDPATH="/usr/local/firstworks/lib"

再run : env-update , 就看到 /etc/ld.so.conf 多了正確的 lib path 了

 

// ———————–
[12/6/13 下午4:37:54] RUTEN AA: 到 /usr/lib 找出原生的 .so 檔,應該會是 libsqlrclientwrapper-0.40.so.xxx.xxx
[12/6/13 下午4:38:13] RUTEN AA: ln -s libsqlrclientwrapper-0.40.so.xxx.xxx 到 loss 的 libsqlrclientwrapper-0.40.so.1.0.0
[12/6/13 下午4:38:17] RUTEN AA: 再 ldconfig
[12/6/13 下午4:40:53] RUTEN AA: 可以用 ldconfig -p 看是不是已經把 libsqlrclientwrapper-0.40.so.1 給 load 進來了