1. 클래스나 파일 이름
클래스나 파일명이 너무 단순하다면 다른 PHP 스크립트와 충돌할 수 있으므로, 고유한 접두어를 붙이는 것이 좋습니다.
개발자의 이름이나 회사의 이름을 접두어로 쓰면 좋습니다.
INCORRECT:
class Email pi.email.php
class Xml ext.xml.php
class Import mod.import.php
CORRECT:
class Pre_email pi.pre_email.php
class Pre_xml ext.pre_xml.php
class Pre_import mod.pre_import.php
class Email pi.email.php
class Xml ext.xml.php
class Import mod.import.php
CORRECT:
class Pre_email pi.pre_email.php
class Pre_xml ext.pre_xml.php
class Pre_import mod.pre_import.php
2. 데이터베이스 테이블 이름
추가적으로 작성하는 모든 테이블은 'exp_' 접두어를 사용하고, 그 뒤에 개발자나 회사 이름으로 식별 가능한 접두어를 넣고, 그 다음 테이블 이름을 간략하게 기술합니다.
INCORRECT:
email_addresses // missing both prefixes
pre_email_addresses // missing exp_ prefix
exp_email_addresses // missing unique prefix
CORRECT:
exp_pre_email_addresses
email_addresses // missing both prefixes
pre_email_addresses // missing exp_ prefix
exp_email_addresses // missing unique prefix
CORRECT:
exp_pre_email_addresses
3. 공백 (Whitespace)
코드에 공백을 넣고 싶을 때 스페이스가 아닌 탭을 사용하는 것이 단계별 들여쓰기에도 좋고, 아주 미세하지만 소스코드의 크기도 줄여줍니다.
4. 코드 들여쓰기
Allman 스타일의 들여쓰기를 사용하세요.
클래스 선언만 제외하고, 중괄호는 항상 한라인을 차지하도록 하며, 자기가 속한 조건문과 같은 레벨의 들여쓰기를 합니다.
INCORRECT:
function foo($bar) {
// ...
}
foreach ($arr as $key => $val) {
// ...
}
if ($foo == $bar) {
// ...
} else {
// ...
}
for ($i = 0; $i < 10; $i++)
{
for ($j = 0; $j < 10; $j++)
{
// ...
}
}
CORRECT:
function foo($bar)
{
// ...
}
foreach ($arr as $key => $val)
{
// ...
}
if ($foo == $bar)
{
// ...
}
else
{
// ...
}
for ($i = 0; $i < 10; $i++)
{
for ($j = 0; $j < 10; $j++)
{
// ...
}
}
function foo($bar) {
// ...
}
foreach ($arr as $key => $val) {
// ...
}
if ($foo == $bar) {
// ...
} else {
// ...
}
for ($i = 0; $i < 10; $i++)
{
for ($j = 0; $j < 10; $j++)
{
// ...
}
}
CORRECT:
function foo($bar)
{
// ...
}
foreach ($arr as $key => $val)
{
// ...
}
if ($foo == $bar)
{
// ...
}
else
{
// ...
}
for ($i = 0; $i < 10; $i++)
{
for ($j = 0; $j < 10; $j++)
{
// ...
}
}
5. 괄호의 여백
일반적으로 괄호에는 공백을 사용하지 말아야 합니다.
가독성을 높이고 함수와 구별하기 위해, PHP 조건문에 매개변수를 사용할때는 예외입니다.
INCORRECT:
$arr[ $foo ] = 'foo';
CORRECT:
$arr[$foo] = 'foo'; // no spaces around array keys
$arr[ $foo ] = 'foo';
CORRECT:
$arr[$foo] = 'foo'; // no spaces around array keys
INCORRECT:
function foo ( $bar )
{
}
CORRECT:
function foo($bar) // no spaces around parenthesis in function declarations
{
}
function foo ( $bar )
{
}
CORRECT:
function foo($bar) // no spaces around parenthesis in function declarations
{
}
INCORRECT:
foreach( $query->result() as $row )
CORRECT:
foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis
foreach( $query->result() as $row )
CORRECT:
foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis
WRITTEN BY
- 손가락귀신
정신 못차리면, 벌 받는다.
,