PHP Code Style Conventions

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 , .


:


  1. , ,
    (isPostRequest, getRequestType, parseSchemaElement, renderPageWithSetupsAndTeardowns)
  2. boolean is, has can


camelCase. camelCase , .


UPPER_CASE_SNAKE_CASE. UPPER_CASE_SNAKE_CASE , .


:



  1. , ,



  2. . Map ($typesMap, $statesMap), .. .


  3. , (unpaidProject)


  4. , , (userIsAdmin, messageIsSend, figureCanBePainted, projectName)


  5. , ,


    :


    $object->expire_at
    $object->setExpireAt($date);
    $object->getExpireAt();

    :


    $object->expiration_date;
    $object->setExpirationDate($date);
    $object->getExpirationDate();

  6. boolean is, has can



  7. :


    if ($project->isInvalid()) {
        // ...
    }
    if ($project->isNotValid()) {
        // ...
    }
    if ($accessManager->isAccessDenied()) {
        // ...
    }

    :


    if (!$project->isValid()) {
        // ...
    }
    if (!$accessManager->isAccessAllowed()) {
        // ...
    }
    if ($accessManager->canAccess()) {
        // ...
    }

  8. ( 8 ), : , ), (userHasRoleAdmin, statusIsActive)




(namespace), ( ). (use).
{...}. .


.


:


  1. .
  2. / — .

.


, .


.


.


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. . .


. , :


  1. . \n, \r, \t ..


, (: type). PHPDoc. , c check validate, .


private. , protected. , public.


null, Null object, , (: ).


jsonreturn true, return ['success' => ['message' => '.....']] ['error' => ['message' => '.....']]. message , .


.


. . , .


boolean . boolean (===), .


:


if ($user) {
    // ...
}
if ($request->postData('amount') == 100) {
    // ...
}
if (!$request->postData('amount')) {
    // ...
}

:


if ($user === null) { // $user   object
    // ...
}
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.

Source: https://habr.com/ru/post/undefined/


All Articles