PHP function naming convention is camel case naming, following the following conventions: 1) Start with a verb or verb phrase; 2) Describe the function; 3) Use specific and meaningful words; 4) Avoid using abbreviations; 5) Keep concise.
PHP function naming convention
In PHP, function naming follows camelCase, which will Capitalize the first letter of a word, lowercase the remaining letters, and capitalize the first letter of each word. In addition, function names should follow the following convention:
createFile()
, calculateTax()
findProductById()
, openConnection()
calculateDiscount()
is better than computeValue()
generateUUID()
is better than genUUID()
Practical case:
// 正确示例: function findUserById(int $userId): User|null { // 获取并返回指定 ID 的用户 }
// 错误示例: function doSomething(int $userId): User|null { // 获取并返回指定 ID 的用户 }
In the error example, the function name doSomething()
is ambiguous and does not accurately describe its function . It also violates the convention of beginning with a verb. The correct example uses the verb findUserById
and provides specific information about what the function does.
The above is the detailed content of What is the naming convention for PHP functions?. For more information, please follow other related articles on the PHP Chinese website!