Naming conventions

I assume you’re already familiar with PSR-1 & PSR-2, which provide PHP recommendations for naming conventions, such as using camelCase for function names.
However, there are a few aspects that aren’t clearly defined there, which can significantly impact your code quality.
Variables and Attributes Naming Style
$camelCase
//or
$under_score
Firstly, it’s crucial to maintain consistency in naming variables. This means you should choose one style and stick with it, avoiding any mix. In most of my projects, and with the teams I’ve collaborated with, we use under_score (also known as snake_case). Why?
- It’s easier to distinguish between variables and function calls
- In databases, column and table names often use underscores. Hence, it’s beneficial to use the same style for object attributes
exception – the const should be named whole in UPPERCASE
const FRIDAY = 5;
const SUNDAY = 7;
Boolean methods
Any boolean-returning method’s name should start with: is.. (e.g. isAdmin), has.. (e.g. hasErrors), or can.. (e.g. canExecute).
For instance, if you want to check if something is valid, don’t name the function “validate” and expect it to return a boolean. The function should be named “isValid”, as “validate” should be used solely to execute the validation, not to check the result.
Single word
If possible, use a single word for your function names—especially for singular operations like: get, fetch, find, save, update, delete, all, first, etc.
It’s also beneficial to consistently use antonyms like: begin, end; first, last; next, previous; min, max; add, delete; up, down; and source, target.
Too Short Names
Please forget about shortcuts or “temp” names. Using such terms is terrible.
$x = ....
//or
$shr_msg = ...
Nobody will know what the intention was. The name should say something about the variable’s data or the function’s purpose.
Too long names
From another perspective, overly long names can also be considered code smells. If you have to use more than three words to name a function, attribute, or variable, then there are two possibilities: you are either doing something wrong or there’s too much logic in your code.
The first possibility – that you’re doing something wrong – could mean, for instance, that you’re using the domain name from your class in your function name, such as:
class User
{
function createUser($data)
{
// ...
}
function findUser($id)
{
// ...
}
function deleteUser($id)
{
// ...
}
}
//then somewhere in our code your method calls look like:
$this->user->createUser($data);
//or
$user->createUser($data);
Since you are within the User class and in adherence to the “Single Responsibility” principle, you shouldn’t create anything other than a user inside of it. Therefore, you shouldn’t include “user” in your function names. The code should look as follows:
class User
{
function create($data)
{
// ...
}
function find($id)
{
// ...
}
function delete($id)
{
// ...
}
}
//then somewhere in our code your method calls look like:
$this->user->create($data);
//or
$user->create($data);