This article discusses an approach to writing and writing PHP code. The following points were formed by analyzing existing company approaches and personal experience.
Rules for naming files and folders
All names for folders and files should be meaningful and speaking (not requiring additional explanation).
Folders
All folders are named lowercase with word separation using the -
minus sign.
If the folder contains classes that belong to the namespace (namespace), then the folder is named according to the namespace namespace (namespace).
Allowed characters for naming folders: Latin letters and a -
minus sign.
Files
All files related to the project are named in lower case with word separation using the symbol -
(minus).
If the file is a class file, it is named according to the class name.
Naming conventions for namespaces, classes, methods, and variables
All names should be meaningful and speaking (not requiring additional explanation).
Namespaces
. , .
PascalCase
. PascalCase
.
Trait
. Interface
. Abstract
.
camelCase
. camelCase
, .
:
- , ,
(isPostRequest
, getRequestType
, parseSchemaElement
, renderPageWithSetupsAndTeardowns
) boolean
is
, has
can
camelCase
. camelCase
, .
UPPER_CASE_SNAKE_CASE
. UPPER_CASE_SNAKE_CASE
, .
:
, ,
. Map
($typesMap
, $statesMap
), .. .
, (unpaidProject
)
, , (userIsAdmin
, messageIsSend
, figureCanBePainted
, projectName
)
, ,
:
$object->expire_at
$object->setExpireAt($date);
$object->getExpireAt();
:
$object->expiration_date;
$object->setExpirationDate($date);
$object->getExpirationDate();
boolean
is
, has
can
:
if ($project->isInvalid()) {
}
if ($project->isNotValid()) {
}
if ($accessManager->isAccessDenied()) {
}
:
if (!$project->isValid()) {
}
if (!$accessManager->isAccessAllowed()) {
}
if ($accessManager->canAccess()) {
}
( 8 ), : , ), (userHasRoleAdmin
, statusIsActive
)
(namespace), ( ). (use
).
{...}
. .
.
:
- .
- / — .
.
, .
.
.
class InterfaceType {
private $property = 'myProp';
public function getProperty():string {
return $this->property;
}
}
(return
) , .
, () .
:
if (IS_REGISTRATOR() && (($params.status === 'W' || $params.status === 'D' || $params.status === 'A') && $params.remark && (($params.subres_level == 0 && ($user_info->selected_title->tid == $params.boss || $user_info->selected_title->tid == $doc_signer_tid || !$params.usertid) || $params.subres_level > 0 && $user_info->selected_title->tid == $params.usertid))) { ... }
:
$docIsInWorkAcceptOrDraft = ...;
$bossHasSignerPriviledge = ...;
$userCanSign = ...;
if ($docIsInWorkAcceptOrDraft && $bossHasSignerPriviledge && $userCanSign) {
}
( ""). , , .
, PHPDoc. - .
//
, /*...*/
.
, , .
, , declare(strict_types=1);
/ null
. 0
.
function sendEmail(string $title, string $message = null, string $date = null): void {
}
$object->sendEmail('Title', null, '2017-01-01');
$object->sendEmail('Title', '', '2017-01-01');
, ( — ).
. array_key_exists
, isset
. . .
. , :
- .
\n
, \r
, \t
..
, (: type
). PHPDoc. , c check
validate
, .
private
. , protected
. , public
.
null
, Null object, , (: ).
json
— return true
, return ['success' => ['message' => '.....']]
['error' => ['message' => '.....']]
. message
, .
.
. . , .
boolean
. boolean
(===
), .
:
if ($user) {
}
if ($request->postData('amount') == 100) {
}
if (!$request->postData('amount')) {
}
:
if ($user === null) {
}
if ((int)$request->postData('amount') === 100) {
}
if ($booking->comment === '') {
}
When using the AND and OR operators at the same time in a conditional expression, it is necessary to give priority to the brackets.